Reputation: 1
i have a table like this
grade | name | price |
---|---|---|
1 | abc | 25 |
1 | abc | 30 |
1 | abc | 35 |
2 | xyz | 40 |
2 | xyz | 45 |
2 | xyz | 50 |
3 | mno | 55 |
3 | mno | 60 |
3 | mno | 65 |
and I want a table like this
grade | abc | xyz | mno |
---|---|---|---|
1 | 25 | 40 | 55 |
2 | 30 | 45 | 60 |
3 | 35 | 50 | 65 |
i tried to transpose the dataframe in pandas but didnt work..
how can i convert this..?
Upvotes: 0
Views: 50
Reputation: 120439
To get your output, your input should be:
>>> df
grade name price # your grade column
0 1 abc 25 # 1
1 2 abc 30 # 1
2 3 abc 35 # 1
3 1 mno 55 # 2
4 2 mno 60 # 2
5 3 mno 65 # 2
6 1 xyz 40 # 3
7 2 xyz 45 # 3
8 3 xyz 50 # 3
If you have the input above, you can use pivot
:
>>> df.pivot('grade', 'name', 'price').reset_index().rename_axis(columns=None)
grade abc mno xyz
0 1 25 55 40
1 2 30 60 45
2 3 35 65 50
Upvotes: 1