how to change one column value to column index?

19 views
Skip to first unread message

进陆

unread,
Oct 28, 2017, 12:31:59 PM10/28/17
to PyData
What I mean, change

week time      class      
Mon morning    math
Mon afternoon   math
Tue morning    reading
Tue afternoon   drawing
Wed morning    
Wed afternoon   reading


to

       
Mon    Tue       Wed
morning   math    reading
afternoon  math   drawing   reading

Can we use pandas do that? Thanks


Pratap Vardhan

unread,
Oct 28, 2017, 1:22:18 PM10/28/17
to PyData
Couple of ways
In [5952]: df.pivot(index='time', columns='week', values='class')
Out[5952]:
week        
Mon      Tue      Wed
time
afternoon  math  drawing  reading
morning    math  reading      
NaN

In [5953]: df.set_index(['time', 'week'])['class'].unstack()
Out[5953]:
week        
Mon      Tue      Wed
time
afternoon  math  drawing  reading
morning    math  reading      
NaN

Pratap Vardhan

unread,
Oct 28, 2017, 1:22:18 PM10/28/17
to PyData
Couple of ways

In [5952]: df.pivot(index='time', columns='week', values='class')
Out[5952]:
week        
Mon      Tue      Wed
time
afternoon  math  drawing  reading
morning    math  reading      
NaN

In [5953]: df.set_index(['time', 'week'])['class'].unstack()
Out[5953]:
week        
Mon      Tue      Wed
time
afternoon  math  drawing  reading
morning    math  reading      
NaN

Regards,
Pratap

On Saturday, October 28, 2017 at 10:01:59 PM UTC+5:30, 进陆 wrote:
Reply all
Reply to author
Forward
0 new messages