Reputation: 167
i have data frame
data=pd.DataFrame({'year':[2010,2010,2010,2011,2011,2011],
'position':[1,2,3,1,2,3],
'name':['a','a','b','c','c','d'],
'points':[34,36,32,14,15,16],
'team':['A','B','C','C','B','A'],
'venue':['ny','ny','ny','cali','cali','cali']})
my code:
data.pivot(index=['position','name'],columns='year',values='points')
It works but for the actual dataset I have to put the year as index otherwise it says duplicate index can we solve I need year in the index and also columns
Upvotes: 2
Views: 175
Reputation: 41387
You can use pivot_table()
which supports duplicates:
data.pivot_table(index=['position', 'name'], columns='year', values='points')
By default, it will average the duplicates. If you prefer a different aggregation, you can specify aggfunc
, e.g. to take the max:
data.pivot_table(index=['position', 'name'], columns='year', values='points',
aggfunc='max')
Upvotes: 2