David
David

Reputation: 1494

Filtering pandas dataframe rows by contains str

I have a python pandas dataframe df with a lot of rows. From those rows, I want to slice out and only use the rows that contain the word 'ball' in the 'body' column. To do that, I can do:

df[df['body'].str.contains('ball')]

The issue is, I want it to be case insensitive, meaning that if the word Ball or bAll showed up, I'll want those as well. One way to do case insensitive search is to turn the string to lowercase and then search that way. I'm wondering how to go about doing that. I tried

df[df['body'].str.lower().contains('ball')]

But that doesn't work. I'm not sure if I'm supposed to use a lambda function on this or something of that nature.

Upvotes: 31

Views: 44718

Answers (2)

rachwa
rachwa

Reputation: 2300

You can also use contains inside query:

In [2]: df = pd.DataFrame({'body': ['Ball', 'cUbE', 'bAll'], 'color': ['red', 'green', 'blue']})

In [3]: df
Out[3]: 
   body  color
0  Ball    red
1  cUbE  green
2  bAll   blue

In [4]: df.query('body.str.contains("ball", case=False).values')
Out[4]: 
   body color
0  Ball   red
2  bAll  blue

If you try to match multiple patterns use |:

In [5]: df.query('body.str.contains("ball|cube", case=False).values')
Out[5]: 
   body  color
0  Ball    red
1  cUbE  green
2  bAll   blue

Upvotes: 0

DSM
DSM

Reputation: 353069

You could either use .str again to get access to the string methods, or (better, IMHO) use case=False to guarantee case insensitivity:

>>> df = pd.DataFrame({"body": ["ball", "red BALL", "round sphere"]})
>>> df[df["body"].str.contains("ball")]
   body
0  ball
>>> df[df["body"].str.lower().str.contains("ball")]
       body
0      ball
1  red BALL
>>> df[df["body"].str.contains("ball", case=False)]
       body
0      ball
1  red BALL
>>> df[df["body"].str.contains("ball", case=True)]
   body
0  ball

(Note that if you're going to be doing assignments, it's a better habit to use df.loc, to avoid the dreaded SettingWithCopyWarning, but if we're just selecting here it doesn't matter.)

(Note #2: guess I really didn't need to specify 'round' there..)

Upvotes: 61

Related Questions