I have a dataframe that looks like this
d = {'Name': ['Sally', 'Sally', 'Sally', 'James', 'James', 'James'], 'Sports': ['Tennis', 'Track & field', 'Dance', 'Dance', 'MMA', 'Crosscountry']}
df = pd.DataFrame(data=d)
Name | Sports |
---|---|
Sally | Tennis |
Sally | Track & field |
Sally | Dance |
James | Dance |
James | MMA |
James | Crosscountry |
It seems that pandas' pivot_table only allows reshaping with numerical aggregation, but I want to reshape it to wide format such that the strings are in the "values":
Name | First_sport | Second_sport | Third_sport |
---|---|---|---|
Sally | Tennis | Track & field | Dance |
James | Dance | MMA | Crosscountry |
Is there a method in pandas that can help me do this? Thanks!