Q#60: Filtering strings in Pandas dataframes

Given the following dataframe, write code using Python (Pandas) to return the rows that contain the string ‘J’ in the name column.

Next, write code to return all rows where favorite_color does not contain the string ‘r’.

TRY IT YOURSELF

https://colab.research.google.com/drive/19ossvu6kBoJvAygmFkzvZOOafHtsg0xo?usp=sharing

ANSWER

This question tests our ability to work with and wrangle Pandas dataframes in python.

The trick to this question is the pandas .str.contains() comparator function. With it we can simply index the original dataframe object with the appropriate conditional statement as follows:

df[df['name'].str.contains('J')]

Next, we can use the unary operator ‘~’ to grab the rows that make the conditional false in the case of selecting rows where favorite_color does not contain the string ‘r’.

df[~df['favorite_color'].str.contains('r')]

--

--