I am using dataframe pivot function as below, but I am getting below error:
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'Serialno': [1,2,2,1,1,2,2,1]
'position': ['G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'],
'points': [5, 7, 7, 9, 4, 9, 9, 12]})
ValueError: Index contains duplicate entries, cannot reshape
Code below:
df.pivot_table(index='team', columns='position', values='points')
When I researched this online I found the root cause is duplicate in data for same index, column combination and below is the fix suggested.
df.pivot_table(index='team', columns='position', values='points', aggfunc='sum')
But in my scenario, I need to apply aggregate function on a new column (which is not part of columns, values). I have a serialno column and in case of duplicate I need to pick the value which has higher serialno.
in above example I want this output :
position F G
team
A 7 7
B 9 9
How can I implement this in above code?