仅供参考,对于这个问题,性能/速度不重要。
我有一个名为cost_table
的现有熊猫数据框...
+----------+---------+------+-------------------------+-----------------+
| material | percent | qty | price_control_indicator | acct_assign_cat |
+----------+---------+------+-------------------------+-----------------+
| abc111 | 1.00 | 50 | v | # |
| abc222 | 0.25 | 2000 | s | # |
| xyz789 | 0.45 | 0 | v | m |
| def456 | 0.9 | 0 | v | # |
| 123xyz | 0.2 | 0 | v | m |
| lmo888 | 0.6 | 0 | v | m |
+----------+---------+------+-------------------------+-----------------+
我需要基于多个字段中的值添加字段cost_source
。
大多数出现在Google上的答案都涉及列表理解或三元运算符,但这些答案仅包括基于一列中值的逻辑。例如,
cost_table['cost_source'] = ['map' if qty > 0 else None for qty in cost_table['qty']]
这是基于一列中的值工作的,但是我不知道如何扩展它以在多列中包含逻辑(或者是否有可能?)。它似乎也不是一个易于阅读/可维护的解决方案。
我尝试将for in
循环与if elif
语句一起使用,但是cost_table['cost_source']
中的值保持不变,并且对于所有行都是None
。但是,如果我在循环中打印每一行,则row['cost_source']
会具有所需的值。
d = {
'material': ['abc111', 'abc222', 'xyz789', 'def456', '123xyz', 'lmo888'],
'percent': [1, .25, .45, .9, .2, .6],
'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)
cost_table['cost_source'] = None
for index, row in cost_table.iterrows():
if (row['qty'] > 0) or (row['price_control_indicator'] == "s") or (row['acct_assign_cat'] == "#"):
row['cost_source'] = "map"
elif (row['percent'] >= 40) and (row['acct_assign_cat'] == "m"):
row['cost_source'] = "vendor"
else:
row['cost_source'] = None
print(row['cost_source']) # outputs map, vendor, or None as expected
print(cost_table)
哪个输出...
+----------+---------+------+-------------------------+-----------------+-------------+
| material | percent | qty | price_control_indicator | acct_assign_cat | cost_source |
+----------+---------+------+-------------------------+-----------------+-------------+
| abc111 | 1.00 | 50 | v | # | None |
| abc222 | 0.25 | 2000 | s | # | None |
| xyz789 | 0.45 | 0 | v | m | None |
| def456 | 0.9 | 0 | v | # | None |
| 123xyz | 0.2 | 0 | v | m | None |
| lmo888 | 0.6 | 0 | v | m | None |
+----------+---------+------+-------------------------+-----------------+-------------+
这是我想要的结果...
+----------+---------+------+-------------------------+-----------------+-------------+
| material | percent | qty | price_control_indicator | acct_assign_cat | cost_source |
+----------+---------+------+-------------------------+-----------------+-------------+
| abc111 | 1.00 | 50 | v | # | map |
| abc222 | 0.25 | 2000 | s | # | map |
| xyz789 | 0.45 | 0 | v | m | vendor |
| def456 | 0.9 | 0 | v | # | map |
| 123xyz | 0.2 | 0 | v | m | None |
| lmo888 | 0.6 | 0 | v | m | vendor |
+----------+---------+------+-------------------------+-----------------+-------------+
答案 0 :(得分:4)
如@bazinga所述,使用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)
答案 1 :(得分:2)
如果您想使用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