基于此答案,有没有办法通过子字符串列表过滤 Pandas 数据框?
假设我想查找 df['menu_item'] 包含
fresh
或 spaghetti
的所有行
没有这样的东西:
df[df['menu_item'].str.contains('fresh') | (df['menu_item'].str.contains('spaghetti')]
str.contains
方法接受正则表达式,因此请使用正则表达式 |
作为 or:
df[df['menu_item'].str.contains('fresh|spaghetti')]
输入示例:
menu_item
0 fresh fish
1 fresher fish
2 lasagna
3 spaghetti o's
4 something edible
输出示例:
menu_item
0 fresh fish
1 fresher fish
3 spaghetti o's
filter_list = ['fresh, 'spaghetti']
df[df['menu_item'].str.contains('|'.join(filter_list)]
输入示例:
menu_item
0 fresh fish
1 fresher fish
2 lasagna
3 spaghetti o's
4 something edible
输出:
menu_item
0 fresh fish
1 fresher fish
3 spaghetti o's