简体   繁体   中英

pivotting in pandas index and column has same column

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

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 , eg to take the max:

data.pivot_table(index=['position', 'name'], columns='year', values='points',
                 aggfunc='max')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM