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
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..)