【问题标题】:Return rows with some text in them and delete the rest [duplicate]返回包含一些文本的行并删除其余的[重复]
【发布时间】:2020-06-29 17:33:07
【问题描述】:

我有一个数据框 df,其值如下:

Common_words         count
0   realdonaldtrump  2932
2   new              2347
3                    2030
4   trump            2013
5   good             1553
6                    1440
7   great            200

我只需要有特定文本的行。例如,需要删除第 3 行和第 6 行等具有空白值的行。

Tried:

df = df.dropna(how='any',axis=0) 

但我仍然得到相同的结果。我觉得这些不是空值而是空格,所以我也在下面尝试:

df.Common_words = df.Common_words.str.replace(' ', '')

但还是一样的结果。第 3 行和第 6 行仍未删除。怎么办?

【问题讨论】:

  • '' 也不是 null/nan ,替换为 np.nan
  • 声明:df.Common_words = df.Common_words.str.replace(' ', np.nan) 给出以下错误TypeError: repl must be a string or callable
  • 使用df.Common_words.replace而不是str.replace

标签: python pandas nlp drop


【解决方案1】:

你可以试试:

df.replace(r'^\s+$', np.nan, regex=True)
df.dropna()

【讨论】:

    【解决方案2】:

    你可以这样做:

    df.Common_words =  df.Common_words.replace(r"\s+", np.NaN, regex=True)
    
    df.dropna()
    

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 2020-07-29
      • 2022-01-17
      • 2012-09-05
      • 2011-09-27
      • 2016-02-12
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多