0

Does anyone know how to use pandas to convert the data frame from the upper one to the lower one? Makes rows of dates become columns of date. Thanks

date        time        price
2020-12-24  17:01:00    10
2020-12-24  17:00:00    11
2020-12-25  17:01:00    11
2020-12-25  17:00:00    12
        
        
time     2020-12-24  2020-12-25
17:01:00    10       11
17:00:00    11       12
Denly
  • 899
  • 1
  • 10
  • 21

2 Answers2

3

Check T

out = df.pivot(*df.columns).T
Out[390]: 
date      2020-12-24  2020-12-25
time                            
17:00:00          11          12
17:01:00          10          11

#df.pivot(columns='date',index='time',values='price')
BENY
  • 317,841
  • 20
  • 164
  • 234
2

Another option using set_index and unstack:

df.set_index(['time', 'date'])['Price'].unstack()

Output:

date      2020-12-24  2020-12-25
time                            
17:00:00          11          12
17:01:00          10          11
Scott Boston
  • 147,308
  • 15
  • 139
  • 187