Home > Back-end >  How can I check if country occurs in string with Pandas?
How can I check if country occurs in string with Pandas?

Time:01-21

I have a pandas dataframe of movies and tv-series from Netflix, one of the columns is the countries where the show is available. This column contains strings that can have multiple countries in it (e.g.'Germany, Czech Republic').

Now I want to find all shows that is from a specific country. This means that I need to check if the country name appears in the string. I would like to solve it like this:

df.loc[distinct_countries[0] in df['country']]

But pandas doesn't support the in operator. I did try to solve this by iterating over every country and movie but since the dataframe is quite large this solution is not efficient enough. If you know a simpler solution that uses pandas, feel free to help me out.

Thanks in advance!

CodePudding user response:

You are probably looking for .str.contains(). It can also handle regular expressions if you want to check for more than one country.

  •  Tags:  
  • Related