1 回答
TA贡献2039条经验 获得超7个赞
用 -
from collections import Counter
df['contents2'] = df['contents'].str.split()
df.groupby(['date', 'categories'])['contents2'].apply(lambda x: Counter(x.sum()))
输出
date categories
2018-01 fish_tank1 Goldfish 2.0
Gombessa 2.0
Goosefish 2.0
Gopher 1.0
Grayling 1.0
carp 1.0
rockfish 1.0
fish_tank2 Goosefish 1.0
Grass 1.0
Grayling 1.0
carp 1.0
mullet 1.0
shark 1.0
2018-02 fish_tank2 Goosefish 3.0
Gopher 1.0
Grass 1.0
Grayling 2.0
carp 2.0
mullet 1.0
rockfish 1.0
shark 1.0
2018-03 fish_tank2 Goosefish 2.0
Gopher 1.0
Grayling 1.0
rockfish 1.0
fish_tank3 Goosefish 1.0
Grass 1.0
Grayling 1.0
carp 1.0
mullet 1.0
shark 1.0
Name: contents2, dtype: float64
添加回答
举报