I have a DataFrame which I am trying to pivot.
df
col_1 col_2 col_3 col_4
John Method 4 White
Tom Method 29613 White
Harry Method 147 White
John Method 84 Blue
Tom Method 28 Blue
John Method 222085 Black
Tom Method 159459 Black
Harry Method 2204225 Black
John Method 600253 Green
Tom Method 3156210 Green
Harry Method 4343635 Green
Harry Method 4343635 Green
Expected result:
newDf
Black Blue Green White
Harry 2204225 8687270 147
John 222085 84 600253 4
Tom 159459 28 3156210 29613
My code:
newDf = pd.pivot_table(df, values='col_3', index=['col_1'], columns=['col_4'], aggfunc={'col_3' : 'sum'})
Column types are as follows:
df.dtypes
col_1 object
col_2 object
col_3 int64
col_4 object
dtype: object
Error:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can anyone please help me here? Thanks!