0

The code is supposed to do a conditional additional based on the values of gender, however the addition did not execute correctly

# Using apply on Male and Female
# Still didnt get the expected result to do addition and sub to the male and female


raw_data = {'Gender_demo': ["Male", "Female", "Male", "Female"],'Price': [100, 200, 300, 400]}
df = pd.DataFrame(raw_data, columns = ['Gender_demo','Price'])

df

def gensub (x, y, z):
    if y == z:
        return x + 75
    else:
        return x


df["Gender_Discount"]=df.Price.apply(gensub, y ="f.Gender_demo", z="Male")

df.head()

What I have

  Gender_demo  Price  Gender_Discount
0        Male    100              100
1      Female    200              200
2        Male    300              300
3      Female    400              400

What I expect is an addition of 75 to the final Gender_Discount variable if gender is male.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jarrod
  • 1

1 Answers1

0

No need a self def function

df['Discount']=df.Price.add(df.Gender_demo.eq('Male')*75,fill_value=0)
df
  Gender_demo  Price  Discount
0        Male    100       175
1      Female    200       200
2        Male    300       375
3      Female    400       400
BENY
  • 317,841
  • 20
  • 164
  • 234