let's assume I have the following example dataframe:
df1 = pd.DataFrame({'col1': ['A', 'A', 'B', 'A'], 'col2': ['CA', 'DA', 'CA', 'CA'], 'col3': [1, 1, 1, 2]})
Out[25]:
col1 col2 col3
0 A CA 1
1 A DA 1
2 B CA 1
3 A CA 2
And I want to group this table by 'col3' as Index, and 'col1' as new columns (in this case columns are 'A' or 'B'), and the values should be 'col2'.
I tried it with pivot_table (also with pivot):
pd.pivot_table(df1,index='col3', columns="col1", values=['col2'])
in this case the error is: No numeric types to aggregate
in case of the pivot
command the error is: multiple indexes
. Both cases seem to be reasonable for me. But is there any option to do it? When I have two values for one columns I prefer the following outcome:
A B
1 ['CA', 'DA'] CA
2 'CA' NaN