Python Pandas更改浮点中的值 [英] Python Pandas alterates values in floating point

查看:77
本文介绍了Python Pandas更改浮点中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Python脚本,该脚本可以加载一个或多个csv文件,将它们连接在一起,然后将整个文件写入一个新的csv文件中.我注意到某些值在此操作过程中被修改了,它们会以非常小的值稍微增加/减少.例如:

I have written a Python script which loads one or several csv files, concatenates them and writes the whole into a single new csv file. I have noticed that certain values are modified during this operation, being slightly incremented/decremented by very small values. As an example:

原始CSV:

Index SomeValue
0.000000    0.000
1.000000    0.000
2.000000    0.000
3.000000    0.000
4.000000    2.527
5.000000    0.000

保存的CSV:

Index SomeValue
0.0 0.0
1.0 0.0
2.0 0.0
3.0 0.0
4.0 2.5269999999999997
5.0 0.0

对我来说,这似乎是一个全面的错误,但我不知道是什么原因引起的.在循环中称为我的脚本"的pandas核心是:

This looks like a full-scale error to me, but I don't know what causes it. The pandas core of my script, which is called in a loop, is:

l_tmpCsv_st = pd.read_csv(l_listElement_tc, sep='\t', index_col=0)
l_listOfCsvFiles_tst.append(l_tmpCsv_st)
# Fills in nan cells with the value "missing" to distinguish betweens a true nan and a missing value due to lacking padding
l_listOfCsvFiles_tst[-1] = l_listOfCsvFiles_tst[-1].fillna(value='missing')

# Concatenating csv file with previous ones
csvFusion = pd.concat([csvFusion, l_listOfCsvFiles_tst[-1]], axis=1)

在循环之后:

# Padding missing values of lower frequency files
csvFusion = csvFusion.fillna(method='pad')
# Determinating which columns need to be deleted (all "Unnamed" columns are panda-error results and need to be removed)
l_listColumnsToDelete_tst = [col for col in csvFusion.columns if 'Unnamed' in col]
# Dropping these columns
csvFusion.drop(l_listColumnsToDelete_tst, axis=1, inplace=True)
# Writing full stuff to file
csvFusion.to_csv(l_endFile_tc, sep='\t', decimal=',', na_rep='-')

脚本的其余部分与熊猫无关,只会损害可读性,因此我将其从复制/粘贴中删除了.

The rest of my script is unrelated to pandas and would only harm readability, thus I have removed it from my copy/paste.

如何避免这个问题?

预先感谢

版本:

这确实是一个浮点错误.将每个值四舍五入到足够高的位数可以解决该问题:

It was indeed a floating point error. Rounding every value to a sufficient high digit solved it:

for col in csvFusion.columns:
    csvFusion[col] = csvFusion[col].round(15)

推荐答案

我认为您需要

I think you need parameter float_format in to_csv, because floating point precission:

print df.to_csv(float_format='%.3f')
Index,SomeValue
0.000,0.000
1.000,0.000
2.000,0.000
3.000,0.000
4.000,2.527
5.000,0.000

我认为您可以使用 round :

I think you can use round:

 df['SomeValue'] = df['SomeValue'].round(3)

这篇关于Python Pandas更改浮点中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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