Reputation: 3
So if my pandas df has two columns, Countries and places:
Countries Places
0 US New York
1 UK Old York
2 France Paris
3 India New Delhi
I have a list like so: l = ['New','Old']
How would I select rows for which the places column contain text that contain string that is also present in my list. (The whole string may or may not be present in the list) (It should create a data frame that only contains, US, UK, India but NOT france). (It will
Upvotes: 0
Views: 3831
Reputation: 120549
Use str.contains
l = ['New', 'Old']
out = df[df['Places'].str.contains('|'.join(l))]
print(out)
# Output
Countries Places
0 US New York
1 UK Old York
3 India New Delhi
Note: str.contains
search in whole string. If you want to limit the search to the start of string, use str.match
instead.
Upvotes: 5