Sveta
Sveta

Reputation: 171

Create a new column in pandas based on values in multiple columns and the same condition

I am trying to create a new column that would be coded as 1 if any value of a set of columns meets the same condition. Here is what I tried and I am getting error. I tried to replace with where statement as well and I would get new_var values replaced during each loop, but I want to keep recoding from the previous steps of the loop (not replaced with each step, just additional recoding). So what I need is: if any value in columns a,b,c is <-0.5 or >0.5, I need New_Var to be coded as 1, otherwise zero.

df = pd.DataFrame(np.random.randn(10, 5), columns=list('abcde'))
df
cols = ['a', 'b', 'c']


def rec_cap(x,y):
    if (x<-0.5) | (x>0.5):
        return 1
    else:
        return y

df['new_var']=0

for p in cols:
    df['new_var']=df.loc[:,[p]].apply(rec_cap, df['new_var'])

Upvotes: 0

Views: 69

Answers (1)

BENY
BENY

Reputation: 323226

IIUC

((df.loc[:,cols].gt(0.5))|(df.loc[:,cols].lt(-0.5))).any(1).astype(int)
Out[564]: 
0    1
1    1
2    1
3    1
4    1
5    1
6    1
7    1
8    1
9    0
dtype: int32

Upvotes: 1

Related Questions