Filter all that rows by matching a substring in all the columns in Pandas df

Viewed 42

I have a dataframe where I expect all the rows that matches a given substring to be written as a list.

df1 = pd.DataFrame({'col1': ["c1", "match", "c3", "c4", "c5", "c6","c7","c8","c9"],  
                'col2': [" ","a1","a2","Smatch6","a4"," ","a8"," ","a7"],  
                   'col3': ["Red", "Ted", "Pet", "match1", "Pmatch", "Rmatchd","sample","agadR","Padgadg"]
                    }) 

The output should go as a list into a Dictionary

Data_Dictionary['data'] = o/p of this

tried this code, but it returns nothing

resultdf = df1.filter(like='match', axis=0)

Expected Result: resultdf

    col1     col2     col3
1   match    a1       Ted
3   c4       Smatch6  match1
5   c6                Rmatchd
2 Answers

You can create boolean mask with Series.str.contains and DataFrame.apply, then check if at least one True per rows by DataFrame.any and filter by boolean indexing:

resultdf = df1[df1.apply(lambda x: x.str.contains('match', na=False)).any(axis=1)]

Or use in in DataFrame.applymap:

resultdf = df1[df1.applymap(lambda x: 'match' in str(x)).any(axis=1)]

print (resultdf)
    col1     col2     col3
1  match       a1      Ted
3     c4  Smatch6   match1
4     c5       a4   Pmatch
5     c6           Rmatchd

A vectorized approach,

>>> df1[df1.sum(axis=1).str.contains('match')]

    col1     col2     col3
1  match       a1      Ted
3     c4  Smatch6   match1
4     c5       a4   Pmatch
5     c6           Rmatchd
Related