hakuna_code
hakuna_code

Reputation: 793

How to perform 'OR' between muliple columns in python dataframe?

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.

Upvotes: 1

Views: 61

Answers (4)

Rohit Lal
Rohit Lal

Reputation: 3309

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']

Upvotes: 1

Oleg O
Oleg O

Reputation: 1065

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)

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

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

Upvotes: 1

jezrael
jezrael

Reputation: 862771

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

Upvotes: 4

Related Questions