0

I have this panda dataframe:

System name Rating Count
system1 1 12
system1 2 156
system1 3 16
systemZ 1 77
systemZ 2 56
systemZ 3 66
systemY 1 99
systemY 2 77
systemY 3 99

How can I split the Rating column so that each new column correspond to a single value in the split column. I.e., I'd like to obtain:

System name 1 2 3
system1 12 156 16
systemZ 77 56 66
systemY 99 77 99

How can I do this with pandas?


To create the dataframe:

import pandas  as pd
df = pd.DataFrame(data={
    'System name': ['system1', 'system1', 'system1', 'systemZ', 'systemZ', 'systemZ', 'systemY', 'systemY',
                    'systemY'], 
    'Rating': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'Count': [12, 156, 16, 77, 56, 66, 99, 77, 99]})
print(df)
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • 1
    `df.pivot(index='System name', columns='Rating', values='Count')`. [pivot documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html) – Ignatius Reilly Apr 18 '23 at 01:54
  • @IgnatiusReilly thanks! I eventually used `df.pivot(index='QA System', columns='Rating', values='Count').reset_index()` (ie, added `.reset_index()` to your code). – Franck Dernoncourt Apr 18 '23 at 02:11

0 Answers0