|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想把现有的数据框改造成按日期列分组的两个数据框,不知道能不能实现,请大神看下,谢谢
data =[["162719","A","2022-12-20","2.1016","-0.72","-0.77"],["004243","C","2022-12-19","2.0891","-0.72","-0.77"],
["519212","B","2022-12-20","1.9733","-0.77","-3.29"],["162411","E","2022-12-19","0.7071","-1.64","-1.46"],
["007844","FC","2022-12-19","0.6967","-1.62","-1.46"],["163208","G","2022-12-20","1.0440","-0.40","-0.40"]]
columns = ["编号","名称","日期","数值","数值2","数值3"]
df=pd.DataFrame(data,columns =columns)
df的样子是 这样的:
编号 名称 日期 数值 数值2 数值3
0 162719 A 2022-12-20 2.1016 -0.72 -0.77
1 004243 C 2022-12-19 2.0891 -0.72 -0.77
2 519212 B 2022-12-20 1.9733 -0.77 -3.29
3 162411 E 2022-12-19 0.7071 -1.64 -1.46
4 007844 FC 2022-12-19 0.6967 -1.62 -1.46
5 163208 G 2022-12-20 1.0440 -0.40 -0.40
我想改造成下面这样:(就是把日期列撤掉,整合到上面的列名里去了)
编号 名称 2022-12-19数值 2022-12-19数值2 2022-12-19数值3 2022-12-20数值 2022-12-20数值2 2022-12-20数值3
0 162719 A 2.1016 -0.72 -0.77
1 004243 C 2.0891 -0.72 -0.77
2 519212 B 1.9733 -0.77 -3.29
3 162411 E 0.7071 -1.64 -1.46
4 007844 FC 0.6967 -1.62 -1.46
5 163208 G
1.0440 -0.40 -0.40
请大神帮忙看看有什么办法没?谢谢
首先你需要用apply()方法把数据列从str格式转换为float格式,然后用上边的仁兄提到的pivot_table()方法。 df['数值']=df['数值'].apply(lambda x: eval(x))
df['数值2']=df['数值'2].apply(lambda x: eval(x))
df['数值3']=df['数值3'].apply(lambda x: eval(x))
d = df.pivot_table(index=['编号','名称'],values=['数值','数值2','数值3'],columns=['日期']).fillna('')
结果(没对齐): 数值 数值 数值2 数值2 数值3 数值3
日期 2022-12-19 2022-12-20 2022-12-19 2022-12-20 2022-12-19 2022-12-20
编号 名称
004243 C 2.0891 -0.72 -0.77
007844 FC 0.6967 -1.62 -1.46
162411 E 0.7071 -1.64 -1.46
162719 A 2.1016 -0.72 -0.77
163208 G 1.044 -0.4 -0.4
519212 B 1.9733 -0.77 -3.29
|
|