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
  • 783
  • 7
  • 16
  • 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
  • 822,522
  • 95
  • 1,334
  • 1,252
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
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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
  • 1,005
  • 6
  • 11
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 Lal
  • 2,791
  • 1
  • 20
  • 36