![](/img/trans.png)
[英]Applying a function on pandas dataframe to perform a sentiment analysis
[英]Sentiment analysis on Dataframe
我有一个数据框“ df”,其数据如下:
State Text
0 California This is a beutiful day# It's too hard I am get...
1 Florida Can somebody please help me; I am new to python
2 New York But I am stuck with code How should I solve th...
该数据帧是使用以下代码从csv文件创建的:
delimiter = ' '
df = df2.groupby('State')['Text'].apply(lambda x: "%s" % delimiter.join(x)).reset_index()
我需要对此数据框“ df”状态进行情感分析(使用TextBlob)。 任何人都可以帮助我明智地进行情绪分析。 我试图这样做:
for row in df.itertuples():
text = df.iloc[:, 1].tolist()
tweets = " ".join(str(x) for x in text)
text = TextBlob(tweets)
score = text.sentiment
但是它给了我总数据帧的情感得分,而不是每个州的情感得分
我的代码给出的输出为:
Sentiment(polarity=-0.07765151515151517, subjectivity=0.49810606060606055)
但是我想分别为每一行输出情感(即针对每个状态)。
您可以将apply()
与lambda
函数结合使用。 这是比循环更有效的方法。
df[['polarity', 'subjectivity']] = df['Text'].apply(lambda Text: pd.Series(TextBlob(Text).sentiment))
返回:
State Text polarity subjectivity
0 California This is a beutiful day# It's too hard I am get -0.291667 0.541667
1 Florida Can somebody please help me; I am new to python 0.136364 0.454545
2 New York But I am stuck with code How should I solve th 0.000000 0.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.