简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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