熊猫数据框情绪分析始终输出相同的值

时间:2019-01-24 20:46:49

标签: python-3.x pandas

希望可以为您提供帮助

我有以下内容-我正在尝试确定csv中一些值的观点。

这似乎一次可以工作,但是当我尝试向pandas数据框添加新列以存储它时,所有值都相同。

有人知道我做错了吗?

In [22]:        import pandas as pd
    ...:        from textblob import TextBlob
    ...: 
    ...:        path = 'Desktop/sentiment.csv'
    ...:        df = pd.read_csv(path, delimiter=',', header='infer')          
    ...: 
    ...:        for row in df.iterrows():
    ...:            blob = TextBlob(str(df.Text))
    ...:            df['sentiment'] = blob.sentiment.polarity
    ...:     

In [23]: df
Out[23]: 
                                              Text  sentiment
0              I love this game, I think its great   0.107143
1  really buggy, not a good experience, do not buy   0.107143
2                        not too bad, not too good   0.107143

In [24]: 

1 个答案:

答案 0 :(得分:3)

代替iterrows,请尝试以下操作:

df['sentiment'] = df.Text.apply(lambda x: TextBlob(str(x)).sentiment.polarity)