dimanche 23 septembre 2018

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?

Aucun commentaire:

Enregistrer un commentaire