Python: How can I make column values as column headers
Problem:
Possible solution (Index NOC)
I believe pivot
is what you are looking for.
raw_data = {'NOC': ['USA', 'USA', 'USA', 'RUS', 'RUS', 'RUS', 'CHN'],
'medal': ['Gold', 'Silver', 'Bronze', 'Gold', 'Silver', 'Bronze', 'Gold'],
'Aantal': ['500', '400', '300', '200', '100', '250', '330']}
df = pd.DataFrame(raw_data)
cont_tab = df.pivot(index='NOC', columns='medal')
cont_tab = cont_tab.fillna(0)
print(cont_tab)
This is the output:
Aantal
medal Bronze Gold Silver
NOC
CHN 0 330 0
RUS 250 200 100
USA 300 500 400
Check out this example for more info.