如果小数为0, pandas 会将float转换为int [英] Pandas convert float to int if decimals are 0

查看:124
本文介绍了如果小数为0, pandas 会将float转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,其中某些列具有数值,而其他列则没有,如下所示:

I have a pandas dataframe, in which some columns have numeric values while others don't, as shown below:

City          a     b       c
Detroit       129   0.54    2,118.00
East          188   0.79    4,624.4712
Houston       154   0.65    3,492.1422
Los Angeles   266   1.00    7,426.00
Miami         26    0.11    792.18
MidWest       56    0.24    772.7813

我想将这些数值四舍五入到小数点后两位,这是我正在使用的:

I want to round off these numeric values to 2 decimal places, for which I am using:

df = df.replace(np.nan, '', regex=True)

之后df变为:

City          a       b       c
Detroit       129.0  0.54   2,118.0
East          188.0  0.79   4,624.47
Houston       154.0  0.65   3,492.14
Los Angeles   266.0  1.0    7,426.0
Miami         26.0   0.11   792.18
MidWest       56.0   0.24   772.78

大多数情况下都可以正常工作,但也可以将整数转换为小数,即将100之类的值四舍五入为100.0.我想要这样的数据框:

It works mostly fine, but it also converts proper integers to decimals, i.e., values like 100 are rounded off to 100.0. I want the dataframe like this:

City          a       b         c
Detroit       129    0.54      2,118
East          188    0.79      4,624.47
Houston       154    0.65      3,492.14
Los Angeles   266    1         7,426
Miami         26     0.11      792.18
MidWest       56     0.24      772.28

我想保留这些值作为适当的整数本身,同时在所有数字列中将其他值四舍五入到小数点后两位.我该怎么办?

I want to keep such values as proper integers itself, while rounding off others to 2 decimal places in all the numeric columns. How can I do that?

推荐答案

使用 g格式 :

常规格式.对于给定的精度p> = 1,这会将数字四舍五入为p个有效数字,然后根据结果的大小以定点格式或科学计数法格式化结果.

General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

精确规则如下:假设以表示类型'e'和精度p-1格式化的结果将具有指数exp.然后,如果-4< = exp<p,该数字使用显示类型'f'和精度p-1-exp进行格式化.否则,该数字将使用显示类型"e"和精度p-1进行格式化.在这两种情况下,除非有效的尾随零都从有效位数中删除,否则如果没有剩余数字,则也删除小数点,除非使用了#"选项.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the '#' option is used.

正负无穷大,正负零和nans的格式分别为inf,-inf,0,-0和nan,无论精度如何.

Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.

精度为0相当于精度为1.默认精度为6.

A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.

df.update(df.select_dtypes(include=np.number).applymap('{:,g}'.format))
print (df)
          City    a     b         c
0      Detroit  129  0.54     2,118
1         East  188  0.79  4,624.47
2      Houston  154  0.65  3,492.14
3  Los Angeles  266     1     7,426
4        Miami   26  0.11    792.18
5      MidWest   56  0.24   772.781

这篇关于如果小数为0, pandas 会将float转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆