转换 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13072259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transforming Pandas dataframe
提问by ast4
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 并转置无济于事,任何提示将不胜感激。
回答by joris
You can use the pivotfunction:
您可以使用该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

