03 ,pd 基础 : dataframe,df 创建,Series =》Dataframe,加行,加列

发布时间:2024-12-17 17:06

瑜伽的阿斯汤加系列适合有一定基础的人练习 #生活常识# #养生常识# #瑜伽养生#

1 ,创建 df ,字典创建 :pd.DataFrame(dict) 代码 :

if __name__ == '__main__': dict = {"name":["wtt","dfl","xiaoya"],"age":[29,32,3]} df01 = pd.DataFrame(dict) print(df01) ============================== age name 0 29 wtt 1 32 dfl 2 3 xiaoya 123456789 2 ,创建空 df :pd.DataFrame(columns=[“name”,“sex”]) 代码 : 创建空的 df

df01 = pd.DataFrame(columns=["name","sex","age","addr"]) print(df01) ============================ Columns: [name, sex, age, addr] Index: [] 12345 3 ,sr =》df ,加列 : df01[“name”] = sr_name 思路 :
1 ,df : 创建空 df ,无数据,有列名
2 ,sr : 创建两个 Series代码 :

if __name__ == '__main__': # 创建空 df : df01 = pd.DataFrame(columns=["name","sex","age"]) # 创建 Series : sr_name = pd.Series(["a","b","c","d","e"]) sr_sex = pd.Series(["m","m","w","w","m"]) sr_age = pd.Series([7,9,5,8,6]) # 组装 : df01["name"] = sr_name df01["sex"] = sr_sex df01["age"] = sr_age # 打印 : print(df01) ============================= name sex age 0 a m 7 1 b m 9 2 c w 5 3 d w 8 4 e m 6

1234567891011121314151617181920 4 ,sr =》df ,加行 : df.append(df01,ignore_index=True) 不建议使用 :因为他产生了新的对象代码 :

if __name__ == '__main__': # 创建空 df : df = pd.DataFrame(columns=["name","sex","age"]) # 创建 Series : sr01 = pd.Series(["a","w",18]) sr02 = pd.Series(["b","m",20]) sr03 = pd.Series(["c","w",16]) # 创建三个子 df df01 = sr01.to_frame().T df01.columns = ["name","sex","age"] df02 = sr02.to_frame().T df02.columns = ["name","sex","age"] df03 = sr03.to_frame().T df03.columns = ["name","sex","age"] df = df.append(df01,ignore_index=True) df = df.append(df02,ignore_index=True) df = df.append(df03,ignore_index=True) print(df) =============================== name sex age 0 a w 18 1 b m 20 2 c w 16

12345678910111213141516171819202122232425

网址:03 ,pd 基础 : dataframe,df 创建,Series =》Dataframe,加行,加列 https://www.yuejiaxmz.com/news/view/500590

相关内容

dataframe常用操作
【记录】创建DataFrame索引
python数据分析
NumPy和Pandas总结
python练习题(三)
(dataframe string 转 float)(object 转 float)
【实用原创】20个Python自动化脚本,解放双手、事半功倍
利用Python进行数据分析——Pandas(2)
有效整理电脑上的文件:清理电脑文件方案需要
figsize = (a, b)

随便看看