pandas 小数位数 精度的处理方法

控制台打印时显示的2位小数:

pd.set_option('precision', 2)

实际修改数据精度:

官例:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.round.html

>>> df = pd.DataFrame(np.random.random([3, 3]),
...  columns=['A', 'B', 'C'], index=['first', 'second', 'third'])
>>> df
    A   B   C
first 0.028208 0.992815 0.173891
second 0.038683 0.645646 0.577595
third 0.877076 0.149370 0.491027
>>> df.round(2)
   A  B  C
first 0.03 0.99 0.17
second 0.04 0.65 0.58
third 0.88 0.15 0.49
>>> df.round({'A': 1, 'C': 2})
   A   B  C
first 0.0 0.992815 0.17
second 0.0 0.645646 0.58
third 0.9 0.149370 0.49
>>> decimals = pd.Series([1, 0, 2], index=['A', 'B', 'C'])
>>> df.round(decimals)
   A B  C
first 0.0 1 0.17
second 0.0 1 0.58
third 0.9 0 0.49

以上这篇pandas 小数位数 精度的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • python通过floor函数舍弃小数位的方法

    本文实例讲述了python通过floor函数舍弃小数位的方法.分享给大家供大家参考.具体分析如下: python中可以通过math库的floor函数来舍弃浮点数后面的小数位 import math print(math.floor( x )) 例如:x=1.2,返回1.0 其返回值为浮点数,如果希望返回整数,可以写成: import math #from jb51.net print(int(math.floor( x ))) 输出结果:1 希望本文所述对大家的Python程序设计有所帮助.

  • 浅谈Python里面小数点精度的控制

    要求较小的精度 round()内置方法 这个是使用最多的,刚看了round()的使用解释,也不是很容易懂.round()不是简单的四舍五入的处理方式. For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done t

  • python 保存float类型的小数的位数方法

    python保留两位小数: In [1]: a = 5.026 In [2]: b = 5.000 In [3]: round(a,2) Out[3]: 5.03 In [4]: round(b,2) Out[4]: 5.0 In [5]: '%.2f' % a Out[5]: '5.03' In [6]: '%.2f' % b Out[6]: '5.00' In [7]: float('%.2f' % a) Out[7]: 5.03 In [8]: float('%.2f' % b) Out[

  • python 正确保留多位小数的实例

    python自带的float函数在保留两位小数的时候不够准确容易出现误差,而('%.2f' % a)的方式是将数字转成了字符串类型,无法进行数字运算,所以这里我们将封装一个方法来实现正确的保留多位小数. from functools import reduce def str2float(strf): def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '

  • python中实现控制小数点位数的方法

    前段时间遇到一个问题,python中怎么设置小数点位数,经过查资料,在这里整理了两种较为简单的方法: 法1:利用python内置的round()函数 a = 1.1314 a = 1.0000 a = 1.1267 b = round(a, 2) b = round(a, 2) b = round(a, 2) output: b=1.13 output: b=1.0 output: b=1.13 法2: a = 1.1314 a = 1.0000 a = 1.1267 b = '%.2f' %

  • python 除法保留两位小数点的方法

    如下所示: a = 1 b = 3 print(a/b) #方法一: print(round(a/b,2)) #方法二: print(format(float(a)/float(b),'.2f')) #方法三: print ('%.2f' %(a/b)) 以上这篇python 除法保留两位小数点的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们.

  • pandas 小数位数 精度的处理方法

    控制台打印时显示的2位小数: pd.set_option('precision', 2) 实际修改数据精度: 官例:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.round.html >>> df = pd.DataFrame(np.random.random([3, 3]), ... columns=['A', 'B', 'C'], index=['first', 'second', 'th

  • python输出小数精度控制的方法

    目录 一.要求较小的精度 1.round()内置方法 2. 使用格式化 二.要求超过17位的精度分析 1. 使用格式化(不推荐) 2. 高精度使用decimal模块,配合getcontext 三.关于小数和取整 1. round() 2. math模块的ceil(x) 3. math模块的floor(x) 一.要求较小的精度 将精度高的浮点数转换成精度低的浮点数. 1.round()内置方法 round()不是简单的四舍五入的处理方式. >>> round(2.5) 2 >>

  • C# 小数位数保留的方法集锦

    1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); provider.NumberDecimalDigits =intDecLength; //要设定的小数位数 double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值转成double this.txtCashAmt.Text =

  • 在JSP中使用formatNumber控制要显示的小数位数方法

    先引入标签库 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 比如保留小数点后两位小数: <fmt:formatNumber value="${xxx}" type="number" maxFractionDigits="2"/> 以上这篇在JSP中使用formatNumber控制要显示的小数位数方法

  • Java指定保留小数位数的方法

    今天项目中需要更改时长的显示方式,规定必须保留两位小数,刚才看简书的时候正好看到一个指定保留小数位数的工具类的文章,在此基础上,做了一点小修改,用起来更加方便了,有需要的朋友尽管撸走 DecimalUtils 类: import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; /** * Created by Sean on 17/3/10. */ public class

  • Python计算开方、立方、圆周率,精确到小数点后任意位的方法

    Python计算的位数 在电脑上做了一个实验,看看python能计算到多少位,一下是结果. x = math.sqrt((3)) print ("%.53f"%(x)) print ("%.63f"%(x)) print ("%.83f"%(x)) 1.73205080756887719317660412343684583902359008789062500 1.732050807568877193176604123436845839023590

  • python DataFrame数据格式化(设置小数位数,百分比,千分位分隔符)

    目录 1.设置小数位数 1.1数据框设置统一小数位数 1.2数据框分别设置不同小数位数 1.3通过Series设置DataFrame小数位数 1.4applymap(自定义函数) 2.设置百分比 3.设置千分位分隔符 1.设置小数位数 1.1 数据框设置统一小数位数 以保留小数点后两位小数为例: import pandas as pd import numpy as np df = pd.DataFrame(np.random.random([5, 5]), columns=['A1', 'A2

  • C++ float转std::string 小数位数控制问题

    目录 float转std::string 小数位数控制 std::stringstream 方式 sprintf 方式 string转float显示位数有误:cout 的 precision 成员函数 问题描述 问题分析 float转std::string 小数位数控制 std::stringstream 方式     float a = 1122.334455;     std::stringstream buf;     buf.precision(2);//覆盖默认精度     buf.s

  • .net decimal保留指定的小数位数(不四舍五入)

    前言 项目中遇到分摊金额的情况,最后一条的金额=总金额-已经分摊金额的和. 这样可能导致最后一条分摊的时候是负数,所以自己写了一个保留指定位数小数的方法. 扩展方法的使用,使得调用起来很优雅. 示例代码 public static class DecimalExtension { /// <summary> /// decimal保留指定位数小数 /// </summary> /// <param name="num">原始数量</param&

  • javascript中的toFixed固定小数位数 简单实例分享

    [code]<script> var a=4.2343; alert(a.toFixed(3)); </script> <script>var a=4.2343;alert(a.toFixed(3));</script>执行结果: toFixed方法将一个数字转换成一个拥有固定小数位数的字符串.

随机推荐