Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

pandas - Filtering DataFrame by list of substrings

Building off this answer, is there a way to filter a Pandas dataframe by a list of substrings?

Say I want to find all rows where df['menu_item'] contains fresh or spaghetti

Without something like this:

df[df['menu_item'].str.contains('fresh') | (df['menu_item'].str.contains('spaghetti')]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The str.contains method you're using accepts regex, so use the regex | as or:

df[df['menu_item'].str.contains('fresh|spaghetti')]

Example Input:

          menu_item
0        fresh fish
1      fresher fish
2           lasagna
3     spaghetti o's
4  something edible

Example Output:

       menu_item
0     fresh fish
1   fresher fish
3  spaghetti o's

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...