I'm pretty new to python and pandas and I'm trying to transform some data. I have a dataset with three columns as shown below:
A B C
col1 21-03-2019 1.2
col2 21-03-2019 23
col3 21-03-2019 45
col4 21-03-2019 2.4
col5 21-03-2019 78
col1 14-07-2019 0.1
col2 14-07-2019 AM
col3 14-07-2019 CDM
col4 14-07-2019 66
col5 14-07-2019 0.1
I'm tyring to pivot the dataframe using B as my index and the pivot table works fine.
import pandas as pd
# creating a dataframe
df = pd.DataFrame({'A': ['col1', 'col2', 'col3', 'col4', 'col5', 'col1', 'col2', 'col3' ,'col4', 'col5'],
'B': [21-03-2019,21-03-2019,21-03-2019,21-03-2019,21-03-2019, 14-07-2019,14-07-2019,14-07-2019,14-07-2019,14-07-2019],
'C': [1.2, 23, 45, 2.4, 78, 0.1, 'AM', 'CDM', 66, 0.1]})
df.pivot(index='B', columns='A', values='C')
A col1 col2 col3 col4 col5
B
21-03-2019 1.2 23 45 2.4 78
14-07-2019 0.1 AM CDM 66 0.1
But there are situations in my dataframe where B column is same for all records, as shown below:
A B C
col1 21-03-2019 1.2
col2 21-03-2019 23
col3 21-03-2019 45
col4 21-03-2019 2.4
col5 21-03-2019 78
col1 21-03-2019 0.1
col2 21-03-2019 AM
col3 21-03-2019 CDM
col4 21-03-2019 66
col5 21-03-2019 0.1
In this case, the pivot fails with the below error:
ValueError: Index contains duplicate entries, cannot reshape
I tried resetting the index (although I don't know what resetting index means) df.pivot(index='B', columns='B', values='C').reset_index('B')
but it's still the same error.
My expected output is:
A col1 col2 col3 col4 col5
B
21-03-2019 1.2 23 45 2.4 78
21-03-2019 0.1 AM CDM 66 0.1
How can I solve this? Can someone please help me?