简体   繁体   中英

Python Pandas - Concatenating rows with same ID by using one column's values as headers

My data set looks something like this

    Faculty ID   Measure ID   Score
0        10001          EDV    high
1        10001        IMM_3      99
2        10001       OP_18b     196
5        10001        OP_22       2
7        10001        OP_29     100
10       10001        PC_01       0
11       10001        SEP_1      56
12       10001   SEP_SH_3HR      89
14       10001  SEV_SEP_3HR      73
15       10001  SEV_SEP_6HR      84
16       10005          EDV    high
17       10005        IMM_3      93
18       10005       OP_18b     141
19       10005       OP_18c     311
21       10005        OP_22       2
22       10005        OP_23     100
23       10005        OP_29      93
26       10005        PC_01       1
27       10005        SEP_1      61
28       10005   SEP_SH_3HR      83
29       10005   SEP_SH_6HR      74
30       10005  SEV_SEP_3HR      83
31       10005  SEV_SEP_6HR      97
32       10006          EDV  medium
33       10006        IMM_3      82
34       10006       OP_18b     176
37       10006        OP_22       2
38       10006        OP_23      58
39       10006        OP_29      80
42       10006        PC_01       0
43       10006        SEP_1      37
44       10006   SEP_SH_3HR      80
46       10006  SEV_SEP_3HR      62
47       10006  SEV_SEP_6HR      74
48       10007          EDV     low
49       10007        IMM_3      72

How would I use Pandas to rearrange the data so the unique ID is the Faculty ID, the headers are the values in the Measure ID column and the values are the scores? For example:

    Faculty ID          EDV   IMM_3   OP_18b   OP_22   OP_22 ...          
0        10001          high     99      196       2       9 ...
1        10005          high     93      141      ...

make use of the pivot

df.pivot(index='Faculty_ID', columns='Measure_ID')
            Score
Measure_ID  EDV     IMM_3    OP_18b     OP_18c  OP_22   OP_23   OP_29   PC_01   SEP_1   SEP_SH_3HR  SEP_SH_6HR  SEV_SEP_3HR     SEV_SEP_6HR
Faculty_ID                                                  
     10001  high    99          196     NaN         2   NaN     100         0      56           89         NaN           73     84
     10005  high    93          141     311         2   100      93         1      61           83          74           83     97
     10006  medium  82          176     NaN         2   58       80         0      37           80         NaN           62     74
     10007  low     72          NaN     NaN       NaN   NaN     NaN       NaN     NaN          NaN         NaN          NaN     NaN

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