【发布时间】:2018-09-01 05:57:19
【问题描述】:
我有一个 pandas 数据框,其中有一列包含 lists:
df = pd.DataFrame({'List': [['once', 'upon'], ['once', 'upon'], ['a', 'time'], ['there', 'was'], ['a', 'time']], 'Count': [2, 3, 4, 1, 2]})
Count List
2 [once, upon]
3 [once, upon]
4 [a, time]
1 [there, was]
2 [a, time]
如何合并List 列并对Count 列求和?预期结果是:
Count List
5 [once, upon]
6 [a, time]
1 [there, was]
我试过了:
df.groupby('List')['Count'].sum()
导致:
TypeError: unhashable type: 'list'
【问题讨论】:
标签: python python-3.x pandas pandas-groupby