Home > Blockchain >  Check every row of a df column for values in a list
Check every row of a df column for values in a list

Time:02-03

The column I'm interested in the dataframe looks like

names=['nonsoluable water', 'water percentage 98% grade', 'special chemical with grade chlorine', 'name with value']

There are other columns too. Those are just numbers/identifiers.

I need every row of the column to be checked if it has any value from the list:

check_for_these = waters, grades, %, chemical

If the column has any of those values from the list, I want it to flag the row in a new column.

I've tried this:

df['names'].apply(lambda x: any([k in x for k in check_for_these]))

but it raises errors/gives wrong output.

And the isin function also raises errors:

df['match'] = df["names"].isin(check_for_these)
print(df)

I want the output to be like the image given below

CodePudding user response:

Try this:

df['flag'] = df['names'].str.contains('|'.join(check_for_these), regex=True, case=False).astype(int)
  •  Tags:  
  • Related