Tie_24
Tie_24

Reputation: 647

Dataframe column based in columns conditions

     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:

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

Answers (3)

jezrael
jezrael

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

jpp
jpp

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

  • Set a default value for the series.
  • Update using .loc for your specific criterion.
  • If you have further conditions, you can switch to using numpy.where as per other answers.

Upvotes: 0

BENY
BENY

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

Related Questions