Returning Actual Data for 'True' Values with Str.Contain

101 views Asked by At

I have a dataframe that's several thousand lines long, structured like below. I want to identify which rows have the string "John" and then I want to return those rows so that I can see the values (not just a row id and "True"/"False).

How can I accomplish this? I've been using the str.contains('John') but that just gives me one column with a bunch of boolean responses which is meaningless to me in that form.

FName
JohnDoe
JaneDoe
SallieMae

1

There are 1 answers

0
Scott Boston On

Let's try to filter data frame to just those names:

df[df.FName.str.contains('John')]

Or this to added a new column to existing dataframe:

df['Johnames'] = df.FName[df.FName.str.contains('John')]

Output:

       FName Johnames
0    JohnDoe  JohnDoe
1    JaneDoe      NaN
2  SallieMae      NaN