为什么python pandas dataframe会四舍五入我的值? [英] Why is python pandas dataframe rounding my values?

查看:1125
本文介绍了为什么python pandas dataframe会四舍五入我的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么pandas数据框会四舍五入我的列中的值,在此我将其他两列的值相除.我希望新列中的数字带有两位小数,但是值是四舍五入的.我已经检查了列的dtype,它们都是"float64".

I do not understand why pandas dataframe is rounding the values in my column where I divide the values of two other columns. I want the numbers in the new colums with two decimals, but the values are rounded. I have checked the dtypes of the columns and both are "float64".

import pandas as pd
import numpy as np


# CURRENT DIRECTORY 
cd = os.path.dirname(os.getcwd())

# concatenate csv files
dfList = []

for root, dirs, files in os.walk(cd):
    for fname in files:
        if re.match("output_contigs_SCMgenes.csv", fname):
            frame = pd.read_csv(os.path.join(root, fname))
            dfList.append(frame)    

df = pd.concat(dfList)

#replace nan in SCM column with 0
df['SCM'].fillna(0, inplace=True)

#add column with genes/SCM
df['genes/SCM'] = df['genes']/df['SCM']

输出如下:

    genome  contig  genes  SCM  genes/SCM
0    20900      48      1    0        inf
1    20900      37    130  103          1
2    20900      35      1    1          1
3    20900       1     79   66          1
4    20900      66      5    3          2

但是我希望我的最后一列不包含舍入值,而是包含至少2位小数的值.

But I want that my last column does not contain rounded values, but values with at least 2 decimals.

推荐答案

我可以通过将pd.options.display.precision设置为0来重现此行为:

I could reproduce this behaviour by setting the pd.options.display.precision to 0:

In [4]: df['genes/SCM'] = df['genes']/df['SCM']

In [5]: df
Out[5]:
   genome  contig  genes  SCM  genes/SCM
0   20900      48      1    0        inf
1   20900      37    130  103   1.262136
2   20900      35      1    1   1.000000
3   20900       1     79   66   1.196970
4   20900      66      5    3   1.666667

In [6]: pd.options.display.precision = 0

In [7]: df
Out[7]:
   genome  contig  genes  SCM  genes/SCM
0   20900      48      1    0        inf
1   20900      37    130  103          1
2   20900      35      1    1          1
3   20900       1     79   66          1
4   20900      66      5    3          2

检查您的熊猫和熊猫脾气暴躁的选项

Check your Pandas & Numpy options

这篇关于为什么python pandas dataframe会四舍五入我的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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