我有以下数据框: Sentence0 Cat is a big lion1 Dogs are descendants of wolf2 Elephants are pachyderm3 Pachyderm animals include rhino, Elephants and hippopotamus我需要创建一个 python 代码,它查看上面句子中的单词,并根据以下不同的数据框计算每个单词的总和。Name Scorecat 1dog 2wolf 2lion 3elephants 5rhino 4hippopotamus 5例如,对于第 0 行,分数将为 1(猫)+ 3(狮子)= 4我希望创建一个如下所示的输出。 Sentence Value0 Cat is a big lion 41 Dogs are descendants of wolf 42 Elephants are pachyderm 53 Pachyderm animals include rhino, Elephants and hippopotamus 14
3 回答
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
首先,您可以尝试一种基于splitandmap的方法,然后使用 计算分数groupby。
v = df1['Sentence'].str.split(r'[\s.!?,]+', expand=True).stack().str.lower()
df1['Value'] = (
v.map(df2.set_index('Name')['Score'])
.sum(level=0)
.fillna(0, downcast='infer'))
df1
Sentence Value
0 Cat is a big lion 4
1 Dogs are descendants of wolf 4 # s/dog/dogs in df2
2 Elephants are pachyderm 5
3 Pachyderm animals include rhino, Elephants and... 14
添加回答
举报
0/150
提交
取消