Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用分类值创建pivot_表?_Python_Python 3.x_Pandas_Pivot Table_Melt - Fatal编程技术网

Python 如何使用分类值创建pivot_表?

Python 如何使用分类值创建pivot_表?,python,python-3.x,pandas,pivot-table,melt,Python,Python 3.x,Pandas,Pivot Table,Melt,在下面的代码中,我有四个分类列 我可以用值(分类列)做透视吗 我有下一个错误: DataError: No numeric types to aggregate 应该使用aggfunc参数定义聚合函数 要获取任何值(例如,如果其唯一): 要连接所有字符串,请执行以下操作: df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True, aggfunc=lambda x: ', '.join

在下面的代码中,我有四个分类列

我可以用值(分类列)做透视吗

我有下一个错误:

DataError: No numeric types to aggregate

应该使用aggfunc参数定义聚合函数

要获取任何值(例如,如果其唯一):

要连接所有字符串,请执行以下操作:

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

非常感谢。你是石头!
df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True, aggfunc='first').reset_index()
df.pivot_table(index=['DATE','COUNTRY'],columns='METRIC',values='VALUE',dropna=True, aggfunc=lambda x: ', '.join(x)).reset_index()