pandas equivalent for R dcast

前端 未结 1 1046
栀梦
栀梦 2021-02-08 15:30

I have some data like this:

import pandas as pd
df = pd.DataFrame(index = range(1,13), columns=[\'school\', \'year\', \'metric\', \'values\'], )
df[\'school\'] =         


        
1条回答
  •  无人及你
    2021-02-08 15:58

    you can use pivot_table():

    In [23]: df2 = (df.pivot_table(index=['school', 'year'], columns='metric',
       ....:                       values='values')
       ....:          .reset_index()
       ....:       )
    
    In [24]:
    
    In [24]: df2
    Out[24]:
    metric school  year  admitsize  avgfinaid  tuition
    0         id1  2015          2          3        1
    1         id1  2016          5          6        4
    2         id2  2015          8          9        7
    3         id2  2016         11         12       10
    

    0 讨论(0)
提交回复
热议问题