使用 python 提取数据框中最接近的键的 json 值

问题描述 投票:0回答:0

我有一个包含产品数量和零件名称的数据框:

Product    Quantity
XXXX       5
YYYY       2
ZZZZ       100

使用来自网站的 api 调用,我根据要以 json 格式购买的数量获得结果价格 as

产品XXXX的返回结果:

'StandardPricing': [{'Quantity': 1, 'UnitPrice': 0.39, 'TotalPrice': 0.39}, {'Quantity': 10, 'UnitPrice': 0.289, 'TotalPrice': 2.89}, {'Quantity': 100, 'UnitPrice': 0.1634, 'TotalPrice': 16.34}, {'Quantity': 500, 'UnitPrice': 0.10824, 'TotalPrice': 54.12}, {'Quantity': 1000, 'UnitPrice': 0.08298, 'TotalPrice': 82.98}]

产品YYYY的返回结果:

'StandardPricing': [{'Quantity': 5000, 'UnitPrice': 0.9, 'TotalPrice': 4500}]
被退回。

产品ZZZZ的返回结果:

'StandardPricing': [{'Quantity': 1, 'UnitPrice': 0.3, 'TotalPrice': 0.3}, {'Quantity': 25, 'UnitPrice': 0.2, 'TotalPrice': 5}, {'Quantity': 500, 'UnitPrice': 0.1, 'TotalPrice': 50}

如果从响应中收到,我需要用最接近的可能数量 [底价] 价格更新我的数据框,否则如果只返回单个结果,则应添加相同的结果。

Product    Quantity    Price    StandardQuantity
XXXX       5           0.39     1
YYYY       2           0.9      5000
ZZZZ       100         0.2      25

在输出表中,由于XXXX prices received from response is 1 or 10 near the required quantity 5, so the value less than 5 is used to fill the table.

如何使用 python 完成此操作?

python json dataframe key-value
© www.soinside.com 2019 - 2024. All rights reserved.