I have a big dataframe containing structure like below:
id year QTY_SOLD QTY_PUR QTY_MFG
0 1 2016 9 2.0 10
1 1 2017 9 0.0 10
2 1 2018 0 2.0 10
3 1 2018 2 1 3
4 2 2016 7 2.0 11
5 2 2017 2 0.0 0
6 2 2018 0 0.0 18
and I created a pivot table where if the value of Quantity sold,purchased and mfg is not zero, the pivot table would look like below:
df['str'] = (df.iloc[:, -3:].astype(bool) * ['S', 'P', 'M']).apply(lambda x: '_'.join([v for v in x if v]), axis=1)
df_pivot = df.pivot(index='id', columns='year', values='str')
year 2016 2017 2018
id
1 S_P_M S_M P_M
2 S_P_M S M
But I am getting below error:
ValueError: Index contains duplicate entries, cannot reshape
Could someone tell me why this could be happening? The dataframe is very big, I have posted only few lines.