Pandas 四舍五入

Pandas rounding

我有以下示例数据框:

Market Value
0  282024800.37
1  317460884.85
2 1260854026.24
3  320556927.27
4   42305412.79

我正在尝试将此数据框中的值四舍五入为最接近的整数。期望的输出:

Market Value

282024800
317460885
1260854026
320556927
42305413

我试过了:

df.values.round()

结果是

Market Value

282025000.00 
317461000.00 
1260850000.00 
320557000.00 
42305400.00 

我做错了什么?

谢谢

这可能更适合作为评论发布,但放在这里是为了格式正确。

我无法生成您的结果。使用 numpy 1.18.1 和 Pandas 1.1.0,

df.round().astype('int')

给我:

   Market Value
0     282024800
1     317460885
2    1260854026
3     320556927
4      42305413

我唯一能想到的是你可能有一个32位系统,其中

df.astype('float32').round().astype('int')

给我

   Market Value
0     282024800
1     317460896
2    1260854016
3     320556928
4      42305412

以下内容将使您的数据信息保持完整,因为 float put 会将其 display/print 精确到最接近的整数。

重要警告: 只能一次将其应用于所有数据帧(这是一个 pandas 宽选项),而不仅仅是单个数据帧。

pd.set_option("display.precision", 0)