Reputation: 791
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.
Upvotes: 6
Views: 5451
Reputation: 139142
You can use the pivot
function:
df.pivot(index='date', columns='variable', values='value')
For more info see: http://pandas.pydata.org/pandas-docs/stable/reshaping.html
Upvotes: 13