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

yipeiwu_com6年前Python基础

控制台打印时显示的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 小数位数 精度的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现删除时保留特定文件夹和文件的示例

实现功能:删除当前目录下,除保留目录和文件外的所有文件和目录 #!bin/env python import os import os.path import shutil def...

python生成式的send()方法(详解)

随便在网上找了找,感觉都是讲半天讲不清楚,这里写一下。 def generator(): while True: receive=yield 1 print('e...

对python打乱数据集中X,y标签对的方法详解

对python打乱数据集中X,y标签对的方法详解

今天踩过的两个小坑: 一.用random的shuffle打乱数据集中的数据-标签对 index=[i for i in range(len(X_batch))] # print(ty...

python2.7删除文件夹和删除文件代码实例

复制代码 代码如下:#!c:\python27\python.exe# -*- coding: utf-8 -*- import osimport re from os import p...

Python中函数参数匹配模型详解

当我们的函数接收参数为任意个,或者不能确定参数个数时,我们,可以利用 * 来定义任意数目的参数,这个函数调用时,其所有不匹配的位置参数会被赋值为元组,我们可以在函数利用循环或索...