1 回答
TA贡献1998条经验 获得超6个赞
您需要分配给具有相同条件的过滤行loc,因此如果条件为,则仅处理行True:
m1 = df_test['product']==0
m2 = df_test['product']==1
df_test.loc[m1, 'made_profit'] = df_test[m1].apply(product0_makes_profit, args=[4000], axis=1, result_type="expand")
df_test.loc[m2, 'made_profit'] = df_test[m2].apply(product1_makes_profit, args=[9000], axis=1, result_type="expand")
print (df_test)
product sold_for made_profit
0 0 5000 True
1 0 4500 True
2 1 10000 True
3 1 8000 False
编辑:
如果返回多个值,function需要Series通过新列名返回索引,还需要创建新列,填充一些默认值(例如NaN)之前loc:
cols = ['made_profit', 'profit_amount']
def product0_makes_profit(row, product0_cost):
return pd.Series([row['sold_for'] > product0_cost, row['sold_for'] - product0_cost], index=cols)
def product1_makes_profit(row, product1_cost):
return pd.Series([row['sold_for'] > product1_cost, row['sold_for'] - product1_cost], index=cols)
for c in cols:
df_test[c] = np.nan
is_prod0 = (df_test['product']==0)
df_test.loc[is_prod0, cols] = df_test[is_prod0].apply(product0_makes_profit, args=[4000], axis=1, result_type="expand")
is_prod1 = (df_test['product']==1)
df_test.loc[is_prod1, cols] = df_test[is_prod1].apply(product1_makes_profit, args=[9000], axis=1, result_type="expand")
print(df_test)
product sold_for made_profit profit_amount
0 0 5000 True 1000.0
1 0 4500 True 500.0
2 1 10000 True 1000.0
3 1 8000 False -1000.0
添加回答
举报