简体   繁体   English

转换Pandas数据帧

[英]Transforming Pandas dataframe

I'm having a little trouble with this maybe someone could direct me in the right direction here. 我有点麻烦,也许有人可以指引我朝着正确的方向前进。

Suppose I have a data frame that looks as follows (actual dataset has many more entries and idents): 假设我有一个如下所示的数据框(实际数据集有更多的条目和标识):

                         open ident
2011-01-01 00:00:00 -1.252090   df1
2011-01-01 01:00:00 -1.427444   df1
2011-01-01 02:00:00 -0.415251   df1
2011-01-01 03:00:00 -0.797411   df1
2011-01-01 04:00:00 -0.515046   df1
2011-01-01 00:00:00  1.107162   df2
2011-01-01 01:00:00  0.073243   df2
2011-01-01 02:00:00  0.224991   df2
2011-01-01 03:00:00 -1.269277   df2
2011-01-01 04:00:00  0.468960   df2

Is there any quick way to reformat the data frame to look as such? 有没有快速的方法来重新格式化数据框看起来像这样?

                         df1        df2
2011-01-01 00:00:00 -1.252090   1.107162   
2011-01-01 01:00:00 -1.427444   0.073243
2011-01-01 02:00:00 -0.415251   0.224991
2011-01-01 03:00:00 -0.797411  -1.269277
2011-01-01 04:00:00 -0.515046   0.468960

I've played around with groupby and transpose to no avail, any tips would be great appreciated. 我玩过groupby并且转置无济于事,任何提示都会非常感激。

You can use the pivot function: 您可以使用pivot功能:

df.pivot(index='date', columns='variable', values='value')

For more info see: http://pandas.pydata.org/pandas-docs/stable/reshaping.html 有关详细信息,请参阅: http//pandas.pydata.org/pandas-docs/stable/reshaping.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM