简体   繁体   English

如何创建带有分类值的数据透视表?

[英]How can I make a pivot_table with categorical values?

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

You should use the aggfunc parameter to define the aggregation function. 您应该使用aggfunc参数定义聚合函数。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM