【发布时间】:2019-01-20 04:23:37
【问题描述】:
代码应该根据性别的值做一个有条件的附加,但是这个附加没有正确执行
# 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()
我有什么
Gender_demo Price Gender_Discount
0 Male 100 100
1 Female 200 200
2 Male 300 300
3 Female 400 400
如果性别为男性,我期望在最终的 Gender_Discount 变量中加上 75。
【问题讨论】:
标签: python python-3.x pandas