Return 行中包含一些文本并删除其余部分
Return rows with some text in them and delete the rest
我有一个数据帧 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 行仍未删除。怎么办?
你可以这样做:
df.Common_words = df.Common_words.replace(r"\s+", np.NaN, regex=True)
df.dropna()
你可以试试:
df.replace(r'^\s+$', np.nan, regex=True)
df.dropna()
我有一个数据帧 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 行仍未删除。怎么办?
你可以这样做:
df.Common_words = df.Common_words.replace(r"\s+", np.NaN, regex=True)
df.dropna()
你可以试试:
df.replace(r'^\s+$', np.nan, regex=True)
df.dropna()