我有以下数据框,CMO($/MWH)是一个最终值,需要使用旧的舍入方法进行舍入(例如:754.275必须舍入到754.28):
INIT_DATE INIT_HOUR CMO_($/MWH)
0 2020-12-01 00:00:00 754.275
1 2020-12-01 01:00:00 728.130
2 2020-12-01 02:00:00 722.575
3 2020-12-01 03:00:00 722.045
4 2020-12-01 04:00:00 721.950
5 2020-12-01 05:00:00 721.035
6 2020-12-01 06:00:00 722.100
7 2020-12-01 07:00:00 739.925
8 2020-12-01 08:00:00 771.390
9 2020-12-01 09:00:00 797.415
10 2020-12-01 10:00:00 796.585
11 2020-12-01 11:00:00 791.875
12 2020-12-01 12:00:00 782.225
13 2020-12-01 13:00:00 783.540
14 2020-12-01 14:00:00 790.980
15 2020-12-01 15:00:00 815.555
16 2020-12-01 16:00:00 824.760
17 2020-12-01 17:00:00 782.265
18 2020-12-01 18:00:00 779.970
19 2020-12-01 19:00:00 784.640
20 2020-12-01 20:00:00 785.380
21 2020-12-01 21:00:00 785.840
22 2020-12-01 22:00:00 779.775
23 2020-12-01 23:00:00 763.775
由于Python 3+使用银行的舍入规则,因此我实现了一个自定义舍入函数,该函数使用了旧的舍入方法:
def _old_round(n):
"""
Since Python 3+ the rounding is made through the Banker's Round Rule,
this function uses the old rounding method (round up if 0.5).
Args:
n (float): A number with 3 thousands values after decimal point
Returns:
(float) : The rounded number now with 2 decimal places, but using
the old round method
Example:
>>> _old_round(782.225)
>>> 782.23
Additional:
For more information about rounding at Python 3, go to:
https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior
"""
# * Multiply ou number by 100 since we want to round the 2 decimal place
new_n = n * 100
# * Round using the Decimal lib, but using the old method
n_dec = Decimal(new_n).quantize(Decimal('1'), rounding=ROUND_HALF_UP)
# * Roll back to original format
return float(n_dec / 100)
当我只使用数字时,该函数正常工作(参见上面的函数示例),但当我使用熊猫应用或映射来执行CMO($/MWH)列中的计算时,有些值没有正确舍入(索引12),有人知道原因吗?在下面的计算中找到DF:
df['ROUNDED_CMO'] = df['CMO_($/MWH)'].apply(_old_round)
df
结果:
INIT_DATE INIT_HOUR CMO_($/MWH) NEW_CMO
0 2020-12-01 00:00:00 754.275 754.28
1 2020-12-01 01:00:00 728.130 728.13
2 2020-12-01 02:00:00 722.575 722.58
3 2020-12-01 03:00:00 722.045 722.05
4 2020-12-01 04:00:00 721.950 721.95
5 2020-12-01 05:00:00 721.035 721.04
6 2020-12-01 06:00:00 722.100 722.10
7 2020-12-01 07:00:00 739.925 739.93
8 2020-12-01 08:00:00 771.390 771.39
9 2020-12-01 09:00:00 797.415 797.42
10 2020-12-01 10:00:00 796.585 796.59
11 2020-12-01 11:00:00 791.875 791.88
12 2020-12-01 12:00:00 782.225 782.22 <
13 2020-12-01 13:00:00 783.540 783.54
14 2020-12-01 14:00:00 790.980 790.98
15 2020-12-01 15:00:00 815.555 815.56
16 2020-12-01 16:00:00 824.760 824.76
17 2020-12-01 17:00:00 782.265 782.27
18 2020-12-01 18:00:00 779.970 779.97
19 2020-12-01 19:00:00 784.640 784.64
20 2020-12-01 20:00:00 785.380 785.38
21 2020-12-01 21:00:00 785.840 785.84
22 2020-12-01 22:00:00 779.775 779.78
23 2020-12-01 23:00:00 763.775 763.78
索引12处的值应为:782.23而不是782.22,该函数适用于所有数据,但由于某些原因,不适用于该特定索引。。。我使用的是Miniconda环境(Windows 10),包括pandas 1.1.3、Python 3.6.12、jupyter notebook 6.1.4(我在这里运行代码)
Obs:我已经尝试使用Math.Ceil、Numpy.Ceil、Decimal-Package-Round选项,以及:Question 1、Question 2、Question 3的解决方案,它们都不能正常工作。我也已经尝试过了(指数12仍然没有四舍五入):
df['ROUNDED_CMO'] = df['CMO_($/MWH)'].map(lambda x : _old_round(x))
df['ROUNDED_CMO'] = df['CMO_($/MWH)'].apply(lambda x : _old_round(x))
对我来说,你的代码工作得很好,也许你的软件包有版本问题。我将向你展示:
pandas
版本:“1.0.5”李>decimal
版本:“1.70”首先,我创建您的数据帧:
然后我应用你的函数:
输出为:
相关问题 更多 >
编程相关推荐