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

前端 未结 1 415
陌清茗
陌清茗 2020-12-15 20:53

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

相关标签:
1条回答
  • 2020-12-15 21:27

    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)
    
    0 讨论(0)
提交回复
热议问题