【问题标题】:Why is python pandas dataframe rounding my values?为什么 python pandas 数据框会舍入我的值?
【发布时间】:2017-04-05 09:24:16
【问题描述】:

我不明白为什么 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']

输出如下:

    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 个小数的值。

【问题讨论】:

  • 我无法复制它。你的 Python、Pandas 和 Numpy 版本是什么?
  • 不,遗憾的是输出仍然相同
  • 我正在使用 Anaconda3
  • 恭喜获得 15 次代表!!!现在您可以投票了,请随时为您认为有用的答案投票。

标签: python pandas dataframe


【解决方案1】:

我可以通过将 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

检查您的 Pandas 和 Numpy 选项

【讨论】:

    【解决方案2】:

    用于以所需的小数点后位数进行四舍五入,例如问题中要求的小数点后 2 位

    df.round({'genes/SCM': 2})
    

    多列

    df.round({'col1_name': 1, 'col2_name': 2})
    

    另外,检查精度未设置为 0,pd.set_option('precision', 5) 可用于适当设置精度。这里 5 是小数点后所需的位数作为示例。

    【讨论】:

      【解决方案3】:

      无法确定,因为我无法重现,但您可以尝试:

      from __future__ import division
      

      在脚本的最顶部。

      【讨论】:

        【解决方案4】:

        尝试使用round()函数

        df['genes/SCM'] = df['genes']/df['SCM'].round(2)
        

        【讨论】:

          【解决方案5】:

          我遇到过类似的问题,如果您从 csv 读取数据,请使用选项float_precision='round_trip' as

          pd.read_csv(resultant_file, sep='\t',float_precision='round_trip')

          It will hold of your precision, if you don't use this option it will limit the precision for speed. - 见@MarkDickinson 评论。

          和 如果与在jupyter notebook中显示数据框有关,则设置精度为display.precisionfollowing

          pd.set_option("precision", 20)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-07-17
            • 1970-01-01
            • 1970-01-01
            • 2018-12-16
            • 2021-05-16
            • 1970-01-01
            • 1970-01-01
            • 2014-09-10
            相关资源
            最近更新 更多