【问题标题】:How to apply TextBlob if value in columns are missing for some rows?如果某些行缺少列中的值,如何应用 TextBlob?
【发布时间】:2018-08-16 19:13:10
【问题描述】:

我有一个如下所示的数据框:

     Text
0    this is amazing
1    nan
2    wow you are great

我想将数据框单元格中的每个单词迭代到 textblob 中,以获取新列中的极性。然而,许多行中有nan

我认为这导致 TextBlob 在新列中为所有行(即使是其中包含文本的行)实现 0.0 的极性分数。

如何对列中的每个文本运行 TextBlob.sentiment.polarity 并使用极性分数创建一个新列?

新的 df 应该是这样的:

     Text                 sentiment
0    this is amazing      0.9
1    nan                  0.0
2    wow you are great    0.8

我不关心nan,所以情绪值可以是nan或0。

当前不工作的代码:

for text in df.columns:
    a = TextBlob(text)
    df['sentiment']=a.sentiment.polarity
    print(df.value)

提前谢谢你。

编辑:

补充一点,不确定这是否有区别,df 上的索引没有重置,因为 df 的其他部分按相同的索引号分组在一起。

【问题讨论】:

    标签: python python-3.x pandas textblob


    【解决方案1】:

    试试这个:

    >>> s=pd.Series(['this is amazing',np.NaN,'wow you are great'],name='Text')
    >>> s
    Out[100]: 
    0      this is amazing
    1                  NaN
    2    wow you are great
    Name: Text, dtype: object
    
    >>> s.apply(lambda x: np.NaN if pd.isnull(x) else TextBlob(x).sentiment.polarity)
    Out[101]: 
    0    0.60
    1     NaN
    2    0.45
    Name: Text, dtype: float64
    

    【讨论】:

      【解决方案2】:

      另一种解决方案:

      d = {'text': ['text1', 'text2', 'text3', 'text4', 'text5'], 'desc': ['The weather is nice today in my city.', 'I hate this weather.', 'Nice weather today.', 'Perfect weather today.', np.NaN]}
      df = pd.DataFrame(data=d)
      print(df)
      
          text                                   desc
      0  text1  The weather is nice today in my city.
      1  text2                   I hate this weather.
      2  text3                    Nice weather today.
      3  text4                 Perfect weather today.
      4  text5                                    NaN
      

      使用 TextBlob 应用情感分析并将结果添加到新列:

      df['sentiment'] = df['desc'].apply(lambda x: 'NaN' if pd.isnull(x) else TextBlob(x).sentiment.polarity)
      print(df)
      
          text                                   desc sentiment
      0  text1  The weather is nice today in my city.       0.6
      1  text2                   I hate this weather.      -0.8
      2  text3                    Nice weather today.       0.6
      3  text4                 Perfect weather today.         1
      4  text5                                    NaN       NaN
      

      【讨论】:

        【解决方案3】:

        如果你对nan有问题,你可以apply你的函数到Text列中没有nan的行,例如:

        mask = df['Text'].notnull() #select the rows without nan
        df.loc[mask,'sentiment'] = df.loc[mask,'Text'].apply(lambda x: TextBlob(x).sentiment.polarity)
        

        注意:我没有TextBlob,所以我从你的代码中假设TextBlob(x).sentiment.polarity 可以工作。

        【讨论】:

        • 谢谢你的两个答案都有效。上面的答案也可以处理 nan
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        相关资源
        最近更新 更多