简体   繁体   中英

Python convert multi-column pandas dataframe to single value table dataframe

I am looking to convert the following multi-level column pandas dataframe to single value table.

Initial table:

    Name    Monica          Rachel      
    Paper    1  2   3        1  2   3

2018-01-01  13  33  15      31  25  33
2018-06-01  11  43  30      36  23  37

New required dataframe table format:

Index        Name   Paper   Scores
2018-01-01  Monica    1       13
2018-01-01  Monica    2       33
2018-01-01  Monica    3       15
2018-06-01  Monica    1       11
2018-06-01  Monica    2       43
2018-06-01  Monica    3       30
2018-01-01  Rachel    1       31
2018-01-01  Rachel    2       25
2018-01-01  Rachel    3       33
2018-06-01  Rachel    1       36
2018-06-01  Rachel    2       23
2018-06-01  Rachel    3       37

I have tried 3 for loops, but could not replicate the Index values as many times.

Like @Wen stated use melt :

df.rename_axis('Index').reset_index().melt('Index', value_name='Score')

Output:

         Index Name Paper  Score
0   2018-01-01    m     1     13
1   2018-06-01    m     1     11
2   2018-01-01    m     2     33
3   2018-06-01    m     2     43
4   2018-01-01    m     3     15
5   2018-06-01    m     3     30
6   2018-01-01    r     1     31
7   2018-06-01    r     1     36
8   2018-01-01    r     2     25
9   2018-06-01    r     2     23
10  2018-01-01    r     3     33
11  2018-06-01    r     3     37

stack and reset_index

df.rename_axis('Index').stack(['Name', 'Paper']).reset_index(name='Scores')

         Index    Name  Paper  Scores
0   2018-01-01  Monica      1      13
1   2018-01-01  Monica      2      33
2   2018-01-01  Monica      3      15
3   2018-01-01  Rachel      1      31
4   2018-01-01  Rachel      2      25
5   2018-01-01  Rachel      3      33
6   2018-06-01  Monica      1      11
7   2018-06-01  Monica      2      43
8   2018-06-01  Monica      3      30
9   2018-06-01  Rachel      1      36
10  2018-06-01  Rachel      2      23
11  2018-06-01  Rachel      3      37

Make it into a Data Frame and then Group By Name and by Paper.

new_table = table.groupby(['Name', 'Paper'])]

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