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
338 views
in Technique[技术] by (71.8m points)

python - Most concise way to select rows where any column contains a string in Pandas dataframe?

What is the most concise way to select all rows where any column contains a string in a Pandas dataframe?

For example, given the following dataframe what is the best way to select those rows where the value in any column contains a b?

df = pd.DataFrame({
    'x': ['foo', 'foo', 'bar'],
    'y': ['foo', 'foo', 'foo'],
    'z': ['foo', 'baz', 'foo']
})

I'm inexperienced with Pandas and the best I've come up with so far is the rather cumbersome df[df.apply(lambda r: r.str.contains('b').any(), axis=1)]. Is there a simpler solution?

Critically, I want to check for a match in any columns, not a particular column. Other similar questions, as best I can tell, only address a single or list of columns.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This question was not given an answer.. but the question itself and the comments has got the answer already which worked really well for me.. and I didn't find the answer anywhereelse I looked.

So I just copy pasted the answer for someone who can find it useful. I added case=False for a case insensitive serach

Solution from @Reason:

the best I've come up with so far is the rather cumbersome

this one worked for me.

df[df.apply(lambda r: r.str.contains('b', case=False).any(), axis=1)] 

Solution from @rbinnun:

this one worked for me for a test dataset.. but for some real data set.. it returned a unicode error as below, but generally a good solution too I think

df[df.apply(lambda row: row.astype(str).str.contains('b', case=False).any(), axis=1)]

takes care of non-string columns, nans, etc.

UnicodeEncodeError: 'ascii' codec can't encode character u'xae' in position 5: ordinal not in range(128)

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

...