0

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

0

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I was giving my two cents with a regex, but I saw your answer. Wouldn't it be better to use startswith instead of contains?
@vaeVictis. You probably right but I don't know if some places around the world end by 'New' or 'Old' :) Note you can also use str.match
In fact, you can't @vaeVictis. startswith does not accept a regex pattern but match works.
@Corralien My bad, I made confusion, but I was suggesting an overcomplicated solution. Thanks for pointing out.
@AdiKrish. Does it solve your problem?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.