'Dynamically checking values in Dataframe

Good Morning,

I have a dataframe with 20+ columns. I'd like to add a new column 'Pattern Exists' such that it is set to 1 if a non-zero value exists in a subset of columns, otherwise it's set to 0. On any row, once a cell with non-zero value is found, we can set 'Pattern Exists' to 1 and move onto the next row.

Is there a way to do this with list comprehension?

df_raw['Pattern Exists'] = 0 ## initiating the column to 0 ##

for row in df_raw.index: ## iterating on dataframe rows ##
  for ptrn in pattern_accuracy['Pattern'].to_list(): ## checking values in the desired columns ##
    if df_raw[ptrn][row] != 0:
      df_raw['Pattern Exists'].loc[row] = 1
      break


Solution 1:[1]

You could do it with one line:

df_raw['Pattern Exists'] = (df_raw[pattern_accuracy['Pattern'].to_list()] != 0).any(axis=1)

The output would be of bool type, you can convert it to int by adding .astype(int)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Z Li