pandas 从df中删除整个行(如果单词出现)

im9ewurl  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(120)

隐藏字列表:
stop_w = [“在”、“&"、“、“、|“”、“、”、“、”、“、”、“、”、“、”、“、”、“、”、“、”、“、”、“、”、“、”]
df:
| 词语|频次|
| - -|- -|
| 该公司|10个|
| 绿色能源|九个|
| 成立于|八个|
| 气体|八个|
| 电学|五个|
我想删除整个行,如果它包含任何给定的停止词,在这个例子中输出应该是:
| 词语|频次|
| - -|- -|
| 绿色能源|九个|
| 电学|五个|

ha5z0ras

ha5z0ras1#

字符|有一个含义,用python的术语来说就是or,所以你需要转义这个含义,以便在你的停用词列表中使用它。
话虽如此,你可以这样做:

stop_w = ["in", "&", "the", "\|", "and", "is", "of", "a", "an", "as", "for", "was"]
df.loc[~df['words'].str.contains('|'.join(stop_w))]

印刷品:

words  frequency
1  green energy          9
4   electricity          5
toiithl6

toiithl62#

您可以按如下方式创建sub_df:

sub_df = df[df.words.str not in stop_w]

或者获取要删除的行的ID:

idx = df[df.words.str in stop_w].index
df.drop(idx)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

相关问题