选择o对应于列和行之间的交点的值

发布于 2025-02-11 05:53:49 字数 508 浏览 2 评论 0原文

PRODUCT VALUE         NUMBER OF PAYMENTS

最小值最大值1至67至11
50010002%4%
1001∞3利息5%

这是付款%利息表的一小部分(实际表为5x5),我需要选择之间的 最终我用有条件的---- 进行了一个价值和许多付款分期付款,这是81行代码,对我来说还可以,但是我一直在想是什么是“专业解决方案”使用表,熊猫或numpy。

例如,如果我的产品售价1500美元,2付款,我的利率将为3%

PRODUCT VALUE         NUMBER OF PAYMENTS

MIN VALUEMAX VALUE1 TO 67 TO 11
50010002%4%
10013%5%

This is a small sample of the table of % interest to pay (the actual table is 5x5), I need to select the interest between a value and a number of payment installments, eventually I did with conditional---- were 81 lines of code, well for me it was ok, but I kept thinking what would be the "professional solution" using tables, pandas or numpy.

For example if my product cost 1500 USD, With 2 payments my interest rate would be 3%

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2025-02-18 05:53:49

使用熊猫很容易。用大整数(例如1000000)替换max value(其中是),然后您可以使用以下方式选择适当的行:

product_value = 1500
df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)]
#    MIN VALUE  MAX VALUE 1 TO 6 7 TO 11
# 1       1001    1000000    3%       5%

困难的部分是从那排。一种方法是创建从列名创建范围:

ranges = { k : range(int(v[0]),int(v[1])+1) for k, v in [ (c, c.split(' TO ')) for c in df.columns ] if len(v) == 2}
print(ranges)
# {'1 TO 6': range(1, 7), '7 TO 11': range(7, 12)}

然后,您可以从该词典中选择列:

no_payments = 2
col = [k for k, v in ranges.items() if no_payments in v][0]
# '1 TO 6'

最后您可以使用行和列索引找到所需的值:

df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)][col].values[0]

output:

3%

注意:鉴于桌子的尺寸很小,您可以通过为每个付款价值的单独列来使生活更轻松。然后,您可以简单地做:

df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)][no_payments].values[0]

Using pandas the first part is easy. Replace MAX VALUE (where it is ) with a large integer (e.g. 1000000) and then you can select the appropriate row using:

product_value = 1500
df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)]
#    MIN VALUE  MAX VALUE 1 TO 6 7 TO 11
# 1       1001    1000000    3%       5%

The hard part is selecting the appropriate column from that row. One way to do that would be to create ranges from the column names:

ranges = { k : range(int(v[0]),int(v[1])+1) for k, v in [ (c, c.split(' TO ')) for c in df.columns ] if len(v) == 2}
print(ranges)
# {'1 TO 6': range(1, 7), '7 TO 11': range(7, 12)}

Then you could select the column from that dictionary:

no_payments = 2
col = [k for k, v in ranges.items() if no_payments in v][0]
# '1 TO 6'

Finally you can find the value you want using the row and column indexes:

df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)][col].values[0]

Output:

3%

Note: given the small size of your table, you could make your life much easier by having a separate column for each number of payments value. Then you could simply do:

df[(df['MIN VALUE'] <= product_value) & (df['MAX VALUE'] >= product_value)][no_payments].values[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文