我有一个函数,应该添加一个新的利润列
def profit(data):
for index, row in data.iterrows():
#print row[0]
profir_margin_L_A = 0.04
profir_margin_E = 0.02
if row[2]== 'Latin America':
# row['profit'] = data.apply(lambda row: row[8]* profir_margin_L_A)
data['profit'] = data['amount_eur_y'] * profir_margin_L_A
else:
# row['profit'] = data.apply(lambda row: row[8]* profir_margin_E)
data['profit'] = data['amount_eur_y'] * profir_margin_E
return data
所有行的返回率都是0.02%,不仅仅是欧洲
我也试过这个,但只适用于一种情况
test['profit'] = (test['amount_eur_y']*profir_margin_L_A).where(test['region'] == 'Latin America')
计算出我需要什么,但当我不能结合欧洲的条件
最后,我需要一个正确的利润计算数据框
您可以使用
numpy.where
创建一个数组,当region == 'Latin America'
和profir_margin_E
不同时,该数组等于profir_margin_L_A
,然后与amount_eur_y
列相乘:相关问题 更多 >
编程相关推荐