Although I found a similar question here (How to move duplicate rows into columns with python), I have an error with the column
parameter.
In my case, I have a dataframe df
and want to change it to df1
:
df = pd.DataFrame(data=[['A',1],['A',2],['B',3],['C',4],['C',5],['C',6]],columns=['Name','Value'])
df
# Name Value
#0 A 1
#1 A 2
#2 B 3
#3 C 4
#4 C 5
#5 C 6
df1
# Name 0 1 2
#0 A 1 2 Nan
#1 B 3 Nan Nan
#2 C 4 5 6
When I tried to run the code, it returned an error as below:
df.pivot(index='Name', columns=range(max(df.pivot_table(columns=['Name'], aggfunc='size'))), values='Value')
#KeyError: 3
I don't know why it couldn't allocate the column name automatically. Can anyone tell me where should I fix the problem in the above code?