【发布时间】:2021-11-12 09:15:23
【问题描述】:
我有一个看起来像这样的 Pandas 数据框。
CustId CustName Price_1 Priceqty1 Price_2 Priceqty2 Price_3 Priceqty3 Price_4 Price_5
5015 Axn 315.12 1 374 1 126.32 3 167.8765
5015 Axn 75.36 3 190.19 7 33.16 1 190.19 88
5015 Axn 123 5 4 18.07 2 0.073 12
7315 Bxy 12.0 4 22.345 3 77.89 1 345.0 3344
7315 Bxy 987.90 7 34.06 4 90.09 3 876.34 908.76
3283 Cxz 123.34 8 55.78 7 12 9 878.09 98.456
3283 Cxz 178.90 7 88 8 0.09 0 987.56
我有 5 个不同的价格列,它们具有不同的值和不同的小数位。为了更好地理解最终用户,我需要将所有价格列的值转换为共同的小数位。
要转换,我需要遵循一定的规则: 1. 应分别为每个客户 ID 进行转换。 2.共同小数位将由小数位最大的价格列值确定。
我们以 CustId 5015 为例
CustId CustName Price_1 Priceqty1 Price_2 Priceqty2 Price_3 Priceqty3 Price_4 Price_5
5015 Axn 315.12 1 374 1 126.32 3 167.8765
5015 Axn 75.36 3 190.19 7 33.16 1 190.19 88
5015 Axn 123 5 4 18.07 2 0.073 12
如果您看到,您可以找到 Price_4 列的第一行的值为 167.8765 此处小数位为 4 。如果您检查 Custid 5015 的所有价格列值,与其他价格列值相比,小数点 4 是最大的一位。由于 4 是最大的,我需要将 custid 5015 的每个价格列值转换为小数点后 4 位。
转换后应该是这个样子。
CustId CustName Price_1 Priceqty1 Price_2 Priceqty2 Price_3 Priceqty3 Price_4 Price_5
5015 Axn 315.1200 1 374.0000 1 126.3200 3 167.8765
5015 Axn 75.3600 3 190.1900 7 33.1600 1 190.1900 88
5015 Axn 123.0000 5 4 18.0700 2 0.0730 12
类似于 custid 5015,custid 7315 的最大十进制值为 3
CustId CustName Price_1 Priceqty1 Price_2 Priceqty2 Price_3 Priceqty3 Price_4 Price_5
7315 Bxy 12.000 4 22.345 3 77.890 1 345.000 3344.000
7315 Bxy 987.900 7 34.060 4 90.090 3 876.340 908.760
空白值只能是空白。
我需要为每个 custid 执行此操作,因为有 800 多个不同的客户 ID。最有效的方法是什么?
【问题讨论】:
标签: python-3.x pandas dataframe python-3.9