1

my df:

No  A   B   C  D
1   1   1   0  1
2   1   1   1  0
3   1   0   1  1
4   1   1   1  1

I would like to perform A or B or C or D and write the result in a column.

No  A   B   C  D Result
1   1   1   0  1 1
2   1   1   1  0 1
3   1   0   1  1 1
4   1   1   1  1 1
...

so the idea is, the result is '1' even if there is one '1' present either in A,B,C or D.

hakuna_code
  • 743
  • 6
  • 15
  • 1
    Does this answer your question? [Logical OR on a subset of columns in a DataFrame](https://stackoverflow.com/questions/31606137/logical-or-on-a-subset-of-columns-in-a-dataframe) – Georgy Dec 10 '19 at 10:12

4 Answers4

4

One idea is use DataFrame.any for test at least one 1 per rows or use max per rows:

#if No is column
df['Result'] = df.iloc[:, 1:].any(axis=1).astype(int)
#if No is index
#df['Result'] = df.any(axis=1).astype(int)

If some another columns:

df['Result'] = df[['A','B','C','D']].any(axis=1).astype(int)

Or:

df['Result'] = df[['A','B','C','D']].max(axis=1)


print (df)

   No  A  B  C  D  Result
0   1  1  1  0  1       1
1   2  1  1  1  0       1
2   3  1  0  1  1       1
3   4  1  1  1  1       1
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
1

Try using:

df['Result'] = df.drop('No', axis=1).max(1)
print(df)

Output:

   No  A  B  C  D  Result
0   1  1  1  0  1       1
1   2  1  1  1  0       1
2   3  1  0  1  1       1
3   4  1  1  1  1       1
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
1

To add to the list of funny ways:

df_a['Result'] = df_a.eval('A + B + C').astype(bool)

The advantage is that eval doesn't make intermediate tables in the memory. If you need explicit int instead of bool, you can cast it, of course:

df_a['Result'] = df_a.eval('A + B + C').astype(bool).astype(int)
Oleg O
  • 955
  • 5
  • 10
1

Strangely, no one mentioned the use of simple | operator.

Answer to your question

df['Result'] = df['A'] | df['B'] | df['C'] | df['D']

Similarly, if you want to perform other operations like AND

df['Result'] = df['A'] & df['B'] & df['C'] & df['D']
Rohit
  • 2,222
  • 1
  • 17
  • 32