I have this dataframe
import numpy as np
import pandas as pd
data = {'month': ['5','5','6', '7'], 'condition': ["yes","no","yes","no"], 'amount': [500,200, 6000, 1873]}
and two values:
inflation5 = 1.05
inflation6 = 1.08
I need to know how can i multiply the cells of column 'amount' by the value inflation5 when the column 'month' value is 5 and the column 'condition' value is "yes", and also multiply the cells of column 'amount' by the value inflation6 when the column 'month' value is 6 and the column 'condition' value is "yes". Thanks
CodePudding user response:
Use numpy.select
:
In [216]: df = pd.DataFrame(data)
In [218]: conds = [(df['condition'].eq('yes') & df['month'].eq('5')), (df['condition'].eq('yes') & df['month'].eq('6'))]
In [219]: inflation5 = 1.05
...: inflation6 = 1.08
In [220]: choices = [df['amount'].mul(inflation5), df['amount'].mul(inflation6)]
In [223]: df['amount'] = np.select(conds, choices, default=df['amount'])
In [224]: df
Out[224]:
month condition amount
0 5 yes 525.0
1 5 no 200.0
2 6 yes 6480.0
3 7 no 1873.0
CodePudding user response:
For this I would run through with an np.where, should make it easily readable, and expandable especially if you wanted to change the condition with a function.
df = pd.DataFrame(data)
df['Inflation'] = np.where((df['month'] == '5') & (df['condition'] == 'yes'), inflation5, 1)
df['Inflation'] = np.where((df['month'] == '6') & (df['condition'] == 'yes'), inflation6, df['Inflation'])
df['Total_Amount'] = df['amount'].values * df['Inflation'].values
CodePudding user response:
I would suggest to use a different approach for efficiency.
Use a dictionary to store the inflations, then you can simply update in a single vectorial call:
inflations = {'5': 1.05, '6': 1.08}
mask = df['condition'].eq('yes')
df.loc[mask, 'amount'] *= df.loc[mask, 'month'].map(inflations)
NB. if you possibly have missing months in the dictionary, use df.loc[mask, 'month'].map(inflations).fillna(1)
in place of df.loc[mask, 'month'].map(inflations)
output:
month condition amount
0 5 yes 525
1 5 no 200
2 6 yes 6480
3 7 no 1873