使用 Pivot/melt 转换 pandas 数据
我有像
import pandas as pd
pd.DataFrame({'Date': ["2022-01-01","2022-01-02","2022-01-03"],
'customer_type': ['A','A','B'],
'process_A': [2,4,5],
'process_B': [3,9,6]})
op
Date customer_type process_A process_B
0 2022-01-01 A 2 3
1 2022-01-02 A 4 9
2 2022-01-03 B 5 6
我希望它以这样的方式转换的数据框,即日期变成列,数据框看起来像这样
customer_type Process 2022-01-01 2022-01-02 2022-01-03
0 A process_A 2 4 0
1 A process_B 3 9 0
2 B process_A 0 0 5
3 B process_B 0 0 6
I have dataframe like
import pandas as pd
pd.DataFrame({'Date': ["2022-01-01","2022-01-02","2022-01-03"],
'customer_type': ['A','A','B'],
'process_A': [2,4,5],
'process_B': [3,9,6]})
op
Date customer_type process_A process_B
0 2022-01-01 A 2 3
1 2022-01-02 A 4 9
2 2022-01-03 B 5 6
what I want it to transform in such a way that Date become column and dataframe look like this
customer_type Process 2022-01-01 2022-01-02 2022-01-03
0 A process_A 2 4 0
1 A process_B 3 9 0
2 B process_A 0 0 5
3 B process_B 0 0 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以组合
熔体
和pivot_table
:输出:
You can combine
melt
andpivot_table
:output: