Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 包含单词列表的列的单词分数总和_Python_String_Pandas_Dataframe - Fatal编程技术网

Python 包含单词列表的列的单词分数总和

Python 包含单词列表的列的单词分数总和,python,string,pandas,dataframe,Python,String,Pandas,Dataframe,我有一个文字专栏: > 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

我有一个文字专栏:

> 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]
另一个情感栏用于每个单词的“情感”值:

> 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

这就是我到目前为止所做的:

df['sentiment_value'] = Sum(df['words'].apply(lambda x: ''.join(x+x for x in sentiment))
预期结果

print(df['sentiment_value'])
0        -5   
1         2   
2        15  
3        -6   
4        -8   
...

若第二列有字符串中的值,那个么首先需要通过转换来过滤数据 一分为二

df['Sentiment'],df['Sentiment_value']=df.sentiment.str.split(" ")

然后,您可以从“情绪”列中找到情绪索引,并从“情绪值”列中获取值

如果第二列的字符串中有值,则需要首先通过转换来过滤数据 一分为二

df['Sentiment'],df['Sentiment_value']=df.sentiment.str.split(" ")

然后您可以从情绪栏中找到情绪指数,并从情绪值栏中获得值,如果您将分数设置为一个系列,以单词作为标签:

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
然后,您可以查找列表的分数:

In [12]: s.loc[["abandon", "abducted"]].sum()
Out[12]: -4
因此,适用的标准是:

df['words'].apply(lambda ls: s.loc[ls])

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

In [21]: s.reindex(["abandon", "abducted", "missing_word"]).sum()
Out[21]: -4.0

df['words'].apply(lambda ls: s.reindex(ls))

如果将分数设置为一个系列,并以单词作为标签:

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
然后,您可以查找列表的分数:

In [12]: s.loc[["abandon", "abducted"]].sum()
Out[12]: -4
因此,适用的标准是:

df['words'].apply(lambda ls: s.loc[ls])

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

In [21]: s.reindex(["abandon", "abducted", "missing_word"]).sum()
Out[21]: -4.0

df['words'].apply(lambda ls: s.reindex(ls))

看起来您错误地加载了以第一个数据行为标题的
情绪。您好,您介意澄清一下您的意思吗?看起来您错误地加载了以第一个数据行为标题的
情绪。您好,您介意澄清一下您的意思吗