为什么 python pandas 数据框四舍五入我的值?

Gra*_*vel 7 python dataframe pandas

我不明白为什么 Pandas 数据框会四舍五入我的列中的值,我将其他两列的值相除。我希望新列中的数字带有两位小数,但这些值是四舍五入的。我检查了列的 dtypes,两者都是“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']
Run Code Online (Sandbox Code Playgroud)

输出如下:

    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
Run Code Online (Sandbox Code Playgroud)

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

Max*_*axU 4

pd.options.display.precision我可以通过设置来重现此行为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
Run Code Online (Sandbox Code Playgroud)

检查您的 Pandas 和 Numpy 选项