我有一个DataFrame,如下

df = pd.DataFrame(data=[[[1,2],[3,4],5,6],[[1,2],[0,0],0,0]],columns=['a','b','c','d'])
df
输出:
	a      b    c d 
0	[1,2] [3,4] 5 6 
1	[1,2] [0,0] 0 0

然后我groupby再apply

df.groupby('a').apply(lambda g:g)

报错!

......
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.factorize()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'list'

把groupby对象改为字符串类型即可恢复正常

df.loc[:,['a','b']] = df.loc[:,['a','b']].astype('str')
df.groupby('a').apply(lambda g:g)
输出:
	a      b    c d 
0	[1,2] [3,4] 5 6 
1	[1,2] [0,0] 0 0
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐