2 回答

TA贡献1111条经验 获得超0个赞
使用df.apply(lambda x: fun(x), 但带有参数axis=1,因此 lambda 函数逐行应用(默认为逐列)。
d = {
'material': ['abc111', 'abc222', 'xyz789', 'def456', '123xyz', 'lmo888'],
'percent': [100, 25, 45, 90, 20, 60],
'qty': [50, 2000, 0, 0, 0, 0],
'price_control_indicator': ['v', 's','v', 'v', 'v', 'v'],
'acct_assign_cat': ['#', '#', 'm', '#', 'm', 'm']
}
cost_table = pd.DataFrame(data=d)
def process_row(row):
if (row['qty'] > 0) or (row['price_control_indicator'] == "s") or (row['acct_assign_cat'] == "#"):
return "map"
elif (row['percent'] >= 40) and (row['acct_assign_cat'] == "m"):
return "vendor"
else:
return None
cost_table['cost_source'] = cost_table.apply(lambda row: process_row(row), axis=1)
print(cost_table)
(我还纠正了一个不一致的地方:在数据procents中大概应该乘以 100)

TA贡献1828条经验 获得超6个赞
如果您想使用 np.select
cond1 = cost_table.qty.gt(0) | cost_table.price_control_indicator.eq('s') | cost_table.acct_assign_cat.eq('#')
cond2 = cost_table.percent.ge(0.4) & cost_table.acct_assign_cat.eq('m')
cost_table['cost_source'] = np.select([cond1, cond2], ['map', 'vendor'], default='None')
print(cost_table)
material percent qty price_control_indicator acct_assign_cat cost_source
0 abc111 1.00 50 v # map
1 abc222 0.25 2000 s # map
2 xyz789 0.45 0 v m vendor
3 def456 0.90 0 v # map
4 123xyz 0.20 0 v m None
5 lmo888 0.60 0 v m vendor
添加回答
举报