[英]Pandas: dataframe transformation using pivot
I have a data frame in the below format: 我有以下格式的数据框:
Date Id A B C D E
2018-01-28 5937.0 11.000000 11.000000 10.000000 10.000000 10.000000
2018-01-21 5937.0 10.000000 10.000000 10.000000 10.000000 10.000000
I want to change the data into the below format: 我想将数据更改为以下格式:
Id 2018-01-28 2018-01-21
A 5937.0 11.000000 10.000000
B 5937.0 11.000000 10.000000
C 5937.0 10.000000 10.000000
D 5937.0 10.000000 10.000000
E 5937.0 10.000000 10.000000
What is the best method to carry out following transformation. 进行以下转换的最佳方法是什么? I have been using pivot but its not working(I am not very good with pivot) 我一直在使用数据透视,但无法正常工作(我对数据透视不太满意)
Use set_index
followed by stack
and unstack
with reset_index
: 使用set_index
其次stack
和unstack
与reset_index
:
df1 = df.set_index(['Date','Id']).stack().unstack(0).reset_index(0)
print(df1)
Date Id 2018-01-21 2018-01-28
A 5937.0 10.0 11.0
B 5937.0 10.0 11.0
C 5937.0 10.0 10.0
D 5937.0 10.0 10.0
E 5937.0 10.0 10.0
df1=df.set_index(['Date','Id']).stack().unstack(0).reset_index(0).rename_axis(None,1)
print(df1)
Id 2018-01-21 2018-01-28
A 5937.0 10.0 11.0
B 5937.0 10.0 11.0
C 5937.0 10.0 10.0
D 5937.0 10.0 10.0
E 5937.0 10.0 10.0
I would do this using melt
and pivot_table
: 我会使用melt
和pivot_table
来做到这pivot_table
:
(df.melt(['Date', 'Id'])
.pivot_table(index=['variable', 'Id'], columns='Date', values='value')
.reset_index())
Date variable Id 2018-01-21 2018-01-28
0 A 5937.0 10.0 11.0
1 B 5937.0 10.0 11.0
2 C 5937.0 10.0 10.0
3 D 5937.0 10.0 10.0
4 E 5937.0 10.0 10.0
Using pivot: 使用枢轴:
(df.pivot_table(values=["A", "B", "C", "D", "E"], columns=["Id", "Date"])
.unstack()
.reset_index(1) # Multi-index level 1 = Id
.rename_axis(None, 1)) # Set columns name to None (not Date)
Output: 输出:
Date Id 2018-01-21 2018-01-28
A 5937.0 10.0 11.0
B 5937.0 10.0 11.0
C 5937.0 10.0 10.0
D 5937.0 10.0 10.0
E 5937.0 10.0 10.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.