简体   繁体   English

如何根据包含条件过滤掉 pandas dataframe 行?

[英]How to filter out pandas dataframe rows based on contains condition?

I have below dataframe我有以下 dataframe

df = pd.DataFrame({'vals': [1, 2, 3, 4, 5], 'ids': ['News: Latest', 'News', 'Cricket', 'Football', 'Football Match: India']})
print(df)

   vals                    ids
0     1           News: Latest
1     2                   News
2     3                Cricket
3     4               Football
4     5  Football Match: India

I have input array, by which i wanted to check df and filter out corresponding data.我有输入数组,我想通过它检查 df 并过滤掉相应的数据。

valueToFilter = ["News", "India"]

Output i want Output 我想要

   vals                    ids
0     1           News: Latest
1     2                   News
2     5  Football Match: India

Can anyone guide me how can i achieve this?谁能指导我如何实现这一目标?

You can try this -你可以试试这个——

>>> import pandas as pd
>>> df = pd.DataFrame({'vals': [1, 2, 3, 4, 5], 'ids': ['News: Latest', 'News', 'Cricket', 'Football', 'Football Match: India']})
>>> valueToFilter = ["News", "India"]
>>> filter_mask = df['ids'].str.contains('|'.join(valueToFilter))
>>> 
>>> df.loc[filter_mask]
   vals                    ids
0     1           News: Latest
1     2                   News
4     5  Football Match: India

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按组过滤掉 Pandas DataFrame 行 - How to filter out Pandas DataFrame rows by group Pandas根据前n行的条件过滤数据帧 - Pandas filter dataframe based on condition for the first n rows 根据 Pandas 中的条件过滤行 - Filter rows based on condition in Pandas 如何根据字符串模式条件删除/删除/过滤熊猫数据框中的行? - How to drop/delete/filter rows in pandas dataframe based on string pattern condition? 如何根据同一列的多个条件过滤掉 pandas.DataFrame 中的多行 - How to filter out multiple rows in a pandas.DataFrame based on multiple conditions for the same column 如何使用 Pandas DataFrame 中的条件过滤汇总行? - How to I filter summed rows with a condition in a Pandas DataFrame? 如何根据条件为pandas数据框中的行组分配唯一值? - How to assign unique values to groups of rows in a pandas dataframe based on a condition? 如何根据条件删除 Pandas 数据框中特定数量的随机行? - How to delete specific number of random rows in Pandas dataframe based on condition? 如何根据有关熊猫数据框中内容的某些条件删除行 - how to drop rows based on some condition about the content in pandas dataframe 如何根据特定条件删除 Pandas DataFrame 的行? - How do I remove rows of a Pandas DataFrame based on a certain condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM