简体   繁体   English

数据框情感分析

[英]Sentiment analysis on Dataframe

I have a dataframe 'df' which has data like : 我有一个数据框“ 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...

This dataframe is created from a csv file using below code : 该数据帧是使用以下代码从csv文件创建的:

delimiter = ' '
df =  df2.groupby('State')['Text'].apply(lambda x: "%s" % delimiter.join(x)).reset_index()

I need to do sentiment analysis(using TextBlob) on this dataframe 'df' state wise. 我需要对此数据框“ df”状态进行情感分析(使用TextBlob)。 Could anyone please help me to do the sentiment analysis state wise. 任何人都可以帮助我明智地进行情绪分析。 I tried to do it as: 我试图这样做:

for row in df.itertuples():
    text = df.iloc[:, 1].tolist()
    tweets = " ".join(str(x) for x in text)
    text = TextBlob(tweets)
    score = text.sentiment

But it gave me sentiment score of total dataframe, not sentiment score for each state separately 但是它给了我总数据帧的情感得分,而不是每个州的情感得分

My code gave output as : 我的代码给出的输出为:

Sentiment(polarity=-0.07765151515151517, subjectivity=0.49810606060606055)

But i want sentiment output for each row(that means for each state) separately. 但是我想分别为每一行输出情感(即针对每个状态)。

You can use apply() in combination with a lambda function. 您可以将apply()lambda函数结合使用。 This is a much more efficient way than looping. 这是比循环更有效的方法。

df[['polarity', 'subjectivity']] = df['Text'].apply(lambda Text: pd.Series(TextBlob(Text).sentiment))

This returns: 返回:

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM