首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含单词列表的列的单词得分之和

包含单词列表的列的单词得分之和
EN

Stack Overflow用户
提问于 2019-02-14 17:24:11
回答 2查看 58关注 0票数 1

我有一个词栏:

代码语言:javascript
复制
> print(df['words'])
0       [awww, thats, bummer, shoulda, got, david, car...   
1       [upset, that, he, cant, update, his, facebook,...   
2       [dived, many, time, ball, managed, save, rest,...   
3       [whole, body, feel, itchy, like, it, on, fire]   
4       [no, it, not, behaving, at, all, im, mad, why,...   
5       [not, whole, crew]

另一栏为每个词的“情感”价值:

代码语言:javascript
复制
> print(sentiment) 
           abandon  -2
0        abandoned  -2
1         abandons  -2
2         abducted  -2
3        abduction  -2
4       abductions  -2
5            abhor  -3
6         abhorred  -3
7        abhorrent  -3
8           abhors  -3
9        abilities   2
...

对于df['words']中的每一行词,我想总结它们各自的情感值。对于情感中不存在的词,等于0。

到目前为止,这就是我所拥有的:

代码语言:javascript
复制
df['sentiment_value'] = Sum(df['words'].apply(lambda x: ''.join(x+x for x in sentiment))

预期结果

代码语言:javascript
复制
print(df['sentiment_value'])
0        -5   
1         2   
2        15  
3        -6   
4        -8   
...
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-14 17:47:31

如果你把分数作为一个系列,用单词作为标签:

代码语言:javascript
复制
In [11]: s  # e.g. sentiment.set_index("word")["score"]
Out[11]:
abandon     -2
abandoned   -2
abandons    -2
abducted    -2
abduction   -2
Name: score, dtype: int64

然后你可以查找一个列表的得分:

代码语言:javascript
复制
In [12]: s.loc[["abandon", "abducted"]].sum()
Out[12]: -4

因此,申请将是:

代码语言:javascript
复制
df['words'].apply(lambda ls: s.loc[ls])

如果您需要支持缺少的单词(而不是s),可以使用reindex:

代码语言:javascript
复制
In [21]: s.reindex(["abandon", "abducted", "missing_word"]).sum()
Out[21]: -4.0

df['words'].apply(lambda ls: s.reindex(ls))
票数 0
EN

Stack Overflow用户

发布于 2019-02-14 17:44:56

如果第二列在字符串中有值,那么首先需要通过将列转换为两列来筛选数据。

代码语言:javascript
复制
df['Sentiment'],df['Sentiment_value']=df.sentiment.str.split(" ")

然后,您可以从情感栏中找到情感索引,从sentiment_value列中获得价值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54695916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档