Python 如何基于另一列中的整数对列中的值进行舍入
我需要在Python中将一列中的价格四舍五入到不同的小数位数。我正在使用此代码创建数据帧df_prices:Python 如何基于另一列中的整数对列中的值进行舍入,python,rounding,Python,Rounding,我需要在Python中将一列中的价格四舍五入到不同的小数位数。我正在使用此代码创建数据帧df_prices: df_prices = pd.DataFrame({'InstrumentID':['001','002','003','004','005','006'], 'Price':[12.44,6.5673,23.999,56.88,4333.22,27.8901],'RequiredDecimals':[2,0,1,2,0,3]}) 数据如下所示: InstrumentID
df_prices = pd.DataFrame({'InstrumentID':['001','002','003','004','005','006'], 'Price':[12.44,6.5673,23.999,56.88,4333.22,27.8901],'RequiredDecimals':[2,0,1,2,0,3]})
数据如下所示:
InstrumentID Price RequiredDecimals
1 12.444 2
2 6.5673 0
3 23.999 1
4 56.88 2
5 4333.22 0
6 27.8901 3
我经常收到此问题的回复:
TypeError:无法将序列转换为
这两种说法都不起作用:
df_prices['PriceRnd'] = np.round(df_prices['Price'] , df_prices['RequiredDecimals'])
df_prices['PriceRnd'] = df_prices['Price'].round(decimals = df_prices['RequiredDecimals'] )
最终输出应该是这样的:
Instrument# Price RequiredDecimals PriceRnd
1 12.444 2 12.44
2 6.5673 0 7
3 23.999 1 24.0
4 56.88 2 56.88
5 4333.22 0 4333
6 27.8901 3 27.890
找不到更好的解决方案,但这个似乎有效
df['Rnd']=[np.around(x,y)表示邮政编码中的x,y(df['Price'],df['requiredecimals'])]
找不到更好的解决方案,但这个似乎有效
df['Rnd']=[np.around(x,y)表示邮政编码中的x,y(df['Price'],df['requiredecimals'])]
虽然不优雅,但您可以试试这个
import pandas as pd
df_prices = pd.DataFrame({'InstrumentID':['001','002','003','004','005','006'], 'Price':[12.44,6.5673,23.999,56.88,4333.22,27.8901],'RequiredDecimals':[2,0,1,2,0,3]})
print(df_prices)
list1 = []
for i in df_prices.values:
list1.append('{:.{}f}' .format(i[1], i[2]))
print(list1)
df_prices["Rounded Price"] =list1
print(df_prices)
InstrumentID Price RequiredDecimals Rounded Price
0 001 12.4400 2 12.44
1 002 6.5673 0 7
2 003 23.9990 1 24.0
3 004 56.8800 2 56.88
4 005 4333.2200 0 4333
5 006 27.8901 3 27.890
还是一行代码
df_prices['Rnd'] = ['{:.{}f}' .format(x, y) for x,y inzip(df_prices['Price'],df_prices['RequiredDecimals'])]
虽然不优雅,但你可以试试这个
import pandas as pd
df_prices = pd.DataFrame({'InstrumentID':['001','002','003','004','005','006'], 'Price':[12.44,6.5673,23.999,56.88,4333.22,27.8901],'RequiredDecimals':[2,0,1,2,0,3]})
print(df_prices)
list1 = []
for i in df_prices.values:
list1.append('{:.{}f}' .format(i[1], i[2]))
print(list1)
df_prices["Rounded Price"] =list1
print(df_prices)
InstrumentID Price RequiredDecimals Rounded Price
0 001 12.4400 2 12.44
1 002 6.5673 0 7
2 003 23.9990 1 24.0
3 004 56.8800 2 56.88
4 005 4333.2200 0 4333
5 006 27.8901 3 27.890
还是一行代码
df_prices['Rnd'] = ['{:.{}f}' .format(x, y) for x,y inzip(df_prices['Price'],df_prices['RequiredDecimals'])]
另一种方法是用适当的因子调整要舍入的数字,然后使用.round()函数始终舍入到最接近的整数这一事实。
df_价格['factor']=10**df_价格['RequiredDecimals']
df_价格['round']=(df_价格['Price']*df_价格['factor'])。round()/df_价格['factor']
四舍五入后,数字将再次除以因子。另一种方法是用适当的因子调整要四舍五入的数字,然后使用.round()-函数始终四舍五入到最接近的整数。
df_价格['factor']=10**df_价格['RequiredDecimals']
df_价格['round']=(df_价格['Price']*df_价格['factor'])。round()/df_价格['factor']
四舍五入后,数字再次除以系数。谢谢,这是第一次成功。“价格”中的值被四舍五入到不同的小数位数。我在这里做错什么了吗?我尝试了你的代码,它为所有值返回了2dp。@Axois
[np.around(x,y)为x,y(df['Price',df['requiredecimals'])]
给了我如下输出:[12.44,7.0,24.0,56.88,4333.0,27.89]
@Axoisnp.around(6.5673,0)
仍然会给我7.0
,,不是像您的示例中那样的7
。@这就是我所认为的情况-OP希望在一列上使用pd.Series.round
函数,使用另一列中的值作为十进制值。如果是这样的话,我的解决方案会像预期的那样工作。谢谢,这是第一次成功。“价格”中的值被四舍五入到不同的小数位数。我在这里做错什么了吗?我尝试了你的代码,它为所有值返回了2dp。@Axois[np.around(x,y)为x,y(df['Price',df['requiredecimals'])]
给了我如下输出:[12.44,7.0,24.0,56.88,4333.0,27.89]
@Axoisnp.around(6.5673,0)
仍然会给我7.0
,,不是像您的示例中那样的7
。@这就是我所认为的情况-OP希望在一列上使用pd.Series.round
函数,使用另一列中的值作为十进制值。如果是这样的话,我的解决方案会像预期的那样工作。