Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for Multiple String Values of Entire Row of Dataframe in python pandas

In a pandas dataframe, I want to search row by row for multiple string values. If the row contains a string value then the function will add/print for that row, into an empty column at the end of the df 1 or 0 based upon
There have been multiple tutorials on how to select rows of a Pandas DataFrame that match a (partial) string.

For Example:

import pandas as pd

#create sample data
data = {'model': ['Lisa', 'Lisa 2', 'Macintosh 128K', 'Macintosh 512K'],
        'launched': [1983,1984,1984,1984],
        'discontinued': [1986, 1985, 1984, 1986]}

df = pd.DataFrame(data, columns = ['model', 'launched', 'discontinued'])
df

I'm pulling the above example from this website: https://davidhamann.de/2017/06/26/pandas-select-elements-by-string/

How would I do a multi-value search of the entire row for: 'int', 'tos', '198'?

Then print into a column next discontinued, a column int that would have 1 or 0 based upon whether the row contained that keyword.

like image 464
BrianBeing Avatar asked Nov 25 '25 13:11

BrianBeing


1 Answers

If you have

l=['int', 'tos', '198']

Then you use str.contains by joining with '|' to get every model that contains any of these words

df.model.str.contains('|'.join(l))

0    False
1    False
2     True
3     True

Edit

If the intention is to check all columns as @jpp interpreted, I'd suggest:

from functools import reduce
res = reduce(lambda a,b: a | b, [df[col].astype(str).str.contains(m) for col in df.columns])

0    False
1     True
2     True
3     True

If you want it as a column with integer values, just do

df['new_col'] = res.astype(int)

     new_col
0    0
1    1
2    1
3    1
like image 198
rafaelc Avatar answered Nov 28 '25 03:11

rafaelc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!