Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I check if country occurs in string with Pandas?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading