Reputation: 647
a b
0 100 90
1 30 117
2 90 99
3 200 94
I want to create a new df["c"]
with next conditions:
If b is into (a ± 0.5a) then c = a
If b is out (a ± 0.5a) then c = b
Output should be:
a b c
0 100 90 100
1 30 117 117
2 90 99 90
3 200 94 94
Upvotes: 3
Views: 54
Reputation: 862441
I think need numpy.where
with conditions created by eval
or chained conditions with &
or between
:
df['c'] = np.where(df.eval("0.5 * a <= b <= 1.5 * a"), df.a, df.b)
#alternative 1
#df['c'] = np.where((df['b'] >= df.a.mul(1.5)) & (df['b'] <= df.a.mul(0.5)), df.a, df.b)
#alternative 2
#df['c'] = np.where(df['b'].between(df.a.mul(0.5), df.a.mul(1.5)), df.a, df.b)
print (df)
a b c
0 100 90 100
1 30 117 117
2 90 99 90
3 200 94 94
Upvotes: 2
Reputation: 164623
This is one way using .loc
accessor and pd.Series.between
.
df['c'] = df['b']
df.loc[df['b'].between(0.5*df['a'], 1.5*df['a']), 'c'] = df['a']
Result
a b c
0 100 90 100
1 30 117 117
2 90 99 90
3 200 94 94
Explanation
.loc
for your specific criterion.numpy.where
as per other answers.Upvotes: 0
Reputation: 323226
You just need where
here , also you two condition is can be treat as one , so we only need one time if--else (which is where
) logic here
df['c']=df['a']
df.c=df.c.where((df.a*1.5>df.b)&(df.a*0.5<df.b),df.b)
df
Out[746]:
a b c
0 100 90 100
1 30 117 117
2 90 99 90
3 200 94 94
Upvotes: 2