0

I have four categorical columns In the following code.

Can I do a pivot with values (categorical column)?

 df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True).reset_index()

I have the next error:

DataError: No numeric types to aggregate
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Hugo Coras
  • 35
  • 6

1 Answers1

1

You should use the aggfunc parameter to define the aggregation function.

To get any value (if its unique for example) :

df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True, aggfunc='first').reset_index()

To concatenate all the strings :

df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True, aggfunc=lambda x: ', '.join(x)).reset_index()
Xavier Canton
  • 216
  • 2
  • 6