I have the following dataset
df = pd.DataFrame([[1,1000],[2,1000],[3,1000]])
df.columns = ["A","B"]
df
A B
0 1 1000
1 2 1000
2 3 1000
I would like to create a new column C
that calculates:
if A = 1 then C = B*.8
if A = 2 then C = B*.1
if A = 3 then C = B*.05
if A = 4 then C = B*.025
...
...(going up to 10)
Is it best to create a function?
def calculation(x):
if x == 1:
return y*0.75
elif...
But im not quite sure how to work with multiple columns. Any help would be appreciated! Thanks
CodePudding user response:
Use Series.map
by dictionary and then multiple by B
column:
d = {1:.8, 2:.1, 3:.05, 4:.025}
df['C'] = df['A'].map(d).mul(df.B)