Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...