现在我有一个像这样的Dataframe:
Stock Price Change
Date
2015-12-31 COVS NaN
2016-01-04 COVS 0.000000
2016-01-05 COVS -0.056000
2016-01-06 COVS -0.008475
2016-01-07 COVS -0.025641
2016-01-08 COVS -0.052632
2015-12-31 EMMS NaN
2016-01-04 EMMS 0.015625
2016-01-05 EMMS -0.030769
2016-01-06 EMMS 0.079365
2016-01-07 EMMS -0.029412
2016-01-08 EMMS -0.045455
2015-12-31 VOD NaN
2016-01-04 VOD -0.002170
2016-01-05 VOD 0.001553
2016-01-06 VOD -0.009615
2016-01-07 VOD -0.009396
2016-01-08 VOD 0.028770
我的问题很简单,如何将其转化为:
Stock COVS EMMS VOD
Date
2015-12-31 NaN NaN NaN
2016-01-04 0.000000 0.015625 -0.002170
2016-01-05 -0.056000 -0.030769 0.001553
2016-01-06 -0.008475 0.079365 -0.009615
2016-01-07 -0.025641 -0.029412 -0.009396
2016-01-08 -0.052632 -0.045455 0.028770
由于这种问题很典型,我相信我会遇到不止一次。
我使用了'Dataframe.T'方法,结果仍然不能满足我。
愿意了解您的解决方案。
答案 0 :(得分:1)
使用pivot
方法。
df.pivot('Date', 'Stock', 'Price Change')
答案 1 :(得分:1)
看看Reshaping and Pivot Tables Docs。
您可以使用以下内容:
df.pivot(index='Date', columns='Stock', values='Price Change')
示例:
In [1]: df
Out[1]:
date variable value
0 2000-01-03 A 0.469112
1 2000-01-04 A -0.282863
2 2000-01-05 A -1.509059
3 2000-01-03 B -1.135632
4 2000-01-04 B 1.212112
5 2000-01-05 B -0.173215
6 2000-01-03 C 0.119209
7 2000-01-04 C -1.044236
8 2000-01-05 C -0.861849
9 2000-01-03 D -2.104569
10 2000-01-04 D -0.494929
11 2000-01-05 D 1.071804
In [2]: df.pivot(index='date', columns='variable', values='value')
Out[2]:
variable A B C D
date
2000-01-03 0.469112 -1.135632 0.119209 -2.104569
2000-01-04 -0.282863 1.212112 -1.044236 -0.494929
2000-01-05 -1.509059 -0.173215 -0.861849 1.071804