Python 使用Pandas重塑包含非数值的数据帧

Python 使用Pandas重塑包含非数值的数据帧,python,r,pandas,reshape,Python,R,Pandas,Reshape,我是一名R用户,目前正在尝试学习python,在我工作的大部分时间里,我需要重塑数据帧,其中每个单元格都包含一个字符串。使用R中restrape2包的dcast进行整形对我来说很容易。我想使用pandas包做一些类似的事情,如下面的脚本: import pandas as pd temp = pd.DataFrame(index=arange(10), columns=['a','b','c','d']) temp['a'] = 'A' temp['b'] = 'B' temp['c'] = '

我是一名R用户,目前正在尝试学习python,在我工作的大部分时间里,我需要重塑数据帧,其中每个单元格都包含一个字符串。使用
R
restrape2
包的
dcast
进行整形对我来说很容易。我想使用
pandas
包做一些类似的事情,如下面的脚本:

import pandas as pd
temp = pd.DataFrame(index=arange(10), columns=['a','b','c','d'])
temp['a'] = 'A'
temp['b'] = 'B'
temp['c'] = 'C'
temp['d'] = 'D'
temp = pd.melt(temp, id_vars=['a','b'])            
temp
pd.pivot_table(temp,index=['a','b'],columns='variable',values='value')
它不断给我错误的
DataError:No numeric type to aggregate
,我认为
aggfunc
是个问题,因为默认值是
np.mean
,是否有其他
aggfunc
列出单元格而不是计算单元格的某些值

pd.pivot_table(temp,index=['a','b'],columns='variable',values='value',
               aggfunc=lambda x: ', '.join(x.unique()))
您可以将自己的函数写入
aggfunc