我有一个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