nia4life
nia4life

Reputation: 363

Conditional w/Pandas Dataframe

Need a help with conditionals for pandas dataframe. Apologies in advance for the basic question or if it's covered elsewhere.

Here's the example dataframe:

employee sales revenue salary
12345    20    10000   100000 

I have a few conditions based on data which will result in salary changing.

scenarios: if sales >10 and revenue > $5,000, increase salary by 20% if sales <5 and revenue > $5,000, increase salary by 10% otherwise, do nothing.

variables:
high_sales = 10
low_sales = 5
high_revenue = 5000
big_increase = 1.2
small_increase = 1.1

I know this requires some nesting but it's not clear to me how to do it.

I want the outcome to be a dataframe with only the salary column adjusted.

Here's the code:

df['salary'] = np.where((df['sales']>=high_sales & df['revenue'] 
>=high_revenue), df['salary'] * big_increase, (df['sales']<=low_sales & 
df['revenue'] >=high_revenue), df['salary'] * small_increase, df['sales'])

Is this right?

Upvotes: 1

Views: 171

Answers (1)

sacuL
sacuL

Reputation: 51335

With multiple conditions, it's nicer to use np.select rather than np.where:

conds = [(df.sales > 10) & (df.revenue > 5000),
         (df.sales < 5) & (df.revenue > 5000)]

choices = [df.salary * 1.2, df.salary * 1.1]

df['salary'] = np.select(conds, choices, default = df.revenue)

Upvotes: 1

Related Questions