[英]python - a simple switch statement IS too much to ask for... trying to pull conditional values - newbie needs direction
I have a situation where I am looking to set unit and extended pricing on a quantity of components on a BOM - there are 1x, 10x and 100+ prices from a vendor as in the example below the kit quantity drives the unit pricing.我有一种情况,我希望为 BOM 上的一定数量的组件设置单位和扩展定价 - 供应商有 1x、10x 和 100+ 的价格,如下面的示例中套件数量驱动单位定价。 I have the spreadsheet in a df but am having an awful time pulling the correct value (1x, 10x, 100x) into the unit price field
我在 df 中有电子表格,但在将正确的值(1x、10x、100x)拉到单价字段中时遇到了可怕的事情
1X 10X 100X Qty Kit_Qty unit ext
0.1 0.062 0.0276 1 7
0.11 0.08 0.0376 1 7
0.1 0.062 0.0276 15 105
0.16 0.117 0.065 15 105
0.1 0.035 0.0158 3 21
0.1 0.055 0.0243 3 21
The above example has 2 items that are qty 7 - the 1x values should be pulled into the unit price field.上面的示例有 2 个数量为 7 的商品 - 应将 1x 值拉入单价字段。 the next has 105 - the 100x price should be selected - the last has 21 - the 10x price... I've generated boolean maps, etc can't seem to map the values to the outputs with the correct conditionals.
下一个有 105 - 应该选择 100 倍的价格 - 最后一个有 21 - 10 倍的价格...我已经生成了 boolean 地图等,map 似乎无法使用正确的条件输出值。 Any pointers would be appreciated.
任何指针将不胜感激。
You can do this by using boolean expressions as integers.您可以通过使用 boolean 表达式作为整数来做到这一点。 True is 1, False is 0.
真为 1,假为 0。
import pandas as pd
data = [
[0.1, 0.062, 0.0276, 1, 7],
[0.11, 0.08, 0.0376, 1, 7],
[0.1, 0.062, 0.0276, 15, 105],
[0.16, 0.117, 0.065, 15, 105],
[0.1, 0.035, 0.0158, 3, 21],
[0.1, 0.055, 0.0243, 3, 21]
]
cols = "1X 10X 100X Qty Kit_Qty".split()
df = pd.DataFrame(data, columns=cols)
print(df)
df['unit'] = \
df['1X'] * (df['Kit_Qty']<10) +\
df['10X'] * (df['Kit_Qty']<100) * (df['Kit_Qty']>=10) +\
df['100X'] * (df['Kit_Qty']>=100)
print(df)
df['ext'] = df['Qty'] * df['unit']
print(df)
Output: Output:
1X 10X 100X Qty Kit_Qty
0 0.10 0.062 0.0276 1 7
1 0.11 0.080 0.0376 1 7
2 0.10 0.062 0.0276 15 105
3 0.16 0.117 0.0650 15 105
4 0.10 0.035 0.0158 3 21
5 0.10 0.055 0.0243 3 21
1X 10X 100X Qty Kit_Qty unit
0 0.10 0.062 0.0276 1 7 0.1000
1 0.11 0.080 0.0376 1 7 0.1100
2 0.10 0.062 0.0276 15 105 0.0276
3 0.16 0.117 0.0650 15 105 0.0650
4 0.10 0.035 0.0158 3 21 0.0350
5 0.10 0.055 0.0243 3 21 0.0550
1X 10X 100X Qty Kit_Qty unit ext
0 0.10 0.062 0.0276 1 7 0.1000 0.100
1 0.11 0.080 0.0376 1 7 0.1100 0.110
2 0.10 0.062 0.0276 15 105 0.0276 0.414
3 0.16 0.117 0.0650 15 105 0.0650 0.975
4 0.10 0.035 0.0158 3 21 0.0350 0.105
5 0.10 0.055 0.0243 3 21 0.0550 0.165
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.