-1

I have a dataframe like this:

enter image description here

I am trying to reshape it like this:

enter image description here

For the life of me, I can't get this right.
I thought using df.pivot_table would work, but it did some sort of averaging of the scores.

I've search SO and can't find a similar question. Most reshaping questions don't want to make a dataset wide.

Any advice is appreciated.

df = [    {'id' : '1', 'score_type': 'test_01', 'score': 1},
          {'id' : '1', 'score_type': 'test_02', 'score': 2},
          {'id' : '1', 'score_type': 'test_03', 'score': 3},
          {'id' : '1', 'score_type': 'test_04', 'score': 4},
          {'id' : '2', 'score_type': 'test_01', 'score': 5},
          {'id' : '2', 'score_type': 'test_02', 'score': 6},
          {'id' : '2', 'score_type': 'test_03', 'score': 7},
          {'id' : '2', 'score_type': 'test_04', 'score': 8}

          ]
df = pd.DataFrame(df)
df = df[['id', 'score_type', 'score']]
df
Monica Heddneck
  • 2,973
  • 10
  • 55
  • 89

2 Answers2

0
df.set_index(['id','score_type']).unstack(-1)
it's-yer-boy-chet
  • 1,917
  • 2
  • 12
  • 21
0

This works for you:

df = df.set_index(['id','score_type']).unstack(-1)
df.columns = df.columns.droplevel()

score_type  test_01  test_02  test_03  test_04
id                                            
1                 1        2        3        4
2                 5        6        7        8

Let's look at the two lines of code in detail:

1 . The first line of code correctly formats the data frame, but adds an extra level 'score' on top:

df = df.set_index(['id','score_type']).unstack(-1)

score                        
score_type test_01 test_02 test_03 test_04
id                                        
1                1       2       3       4
2                5       6       7       8

2 . The second line of code allows you to delete the added level you are not interested in and get the result you were looking for:

df.columns = df.columns.droplevel()

score_type  test_01  test_02  test_03  test_04
id                                            
1                 1        2        3        4
2                 5        6        7        8
Massifox
  • 4,369
  • 11
  • 31