3

I need to apply a single condition to 3 columns of a dataframe and change value of 4th without using or statement .

I can do with np.where but if the no of columns is big it's going to take a lot of time

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[1,2,3,4],'b':[1,3,6,7],'c':[4,6,4,1], 'd':['p','f','p','u'],'e':['a','a','b','c']})

df['d'] = np.where(df.a > 4 | df.b > 4 | df.c > 4 , 'p',df['d']) 

df = pd.DataFrame({'a':[1,2,3,4,5],'b':[1,3,6,7],'c':[4,6,4,1], 'd':['p','f','p','f']})
df['d']=np.where(df.a > 4 | df.b > 4 | df.c > 4 , 'p','f') 

I need someway of implementing same condition > , < to list of columns without using or for each.

5
  • Possible duplicate of selecting across multiple columns with python pandas? Commented Feb 13, 2019 at 13:20
  • I'm not sure what you are tying to accomplish. Are you trying to change the value of the fourth column if the condition of applied to the three columns is true? Commented Feb 13, 2019 at 13:40
  • Yes right but I want to do it without using multiple or.. The actual column has 30 + columns and 25th column will change if 20 columns have data on all rows > 250. Using 20 or is not practical Commented Feb 14, 2019 at 5:23
  • your condition is same for all column? like you want all columns to be greater than same value(i.e. 4 in your example)? Commented Feb 14, 2019 at 5:52
  • yes . right .. just to note columns involved for condition checking is not equal to the all columns in the dataframe. Commented Feb 14, 2019 at 5:57

1 Answer 1

2

Use DataFrame.gt along with np.where:

import numpy as np
import pandas as pd 

df = pd.DataFrame({'a':[1,2,3,4],'b':[1,3,6,7],'c':[4,6,4,1], 'd':['p','f','p','u'],'e':['a','a','b','c']})

# create a subset of a dataframe on which you want to check condition
new_df = df[['a','b','c']]
mask = new_df.gt(4).any(axis=1)  # check if any value is greater than 4

df['d'] = np.where(mask, 'p','f')

print(df)

Output:

   a  b  c  d  e                                                                                                                      
0  1  1  4  f  a                                                                                                                      
1  2  3  6  p  a                                                                                                                      
2  3  6  4  p  b                                                                                                                      
3  4  7  1  p  c  
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.