pandas 错误的十进制计算 [英] Wrong decimal calculations with pandas

查看:64
本文介绍了 pandas 错误的十进制计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫中有一个包含四列的数据框(df),我希望有一个新列来代表这四列的平均值:df ['mean'] = df.mean(1)

I have a data frame (df) in pandas with four columns and I want a new column to represent the mean of this four columns: df['mean']= df.mean(1)

  1    2    3    4   mean 
NaN  NaN  NaN  NaN      NaN  
5.9  5.4  2.4  3.2    4.225  
0.6  0.7  0.7  0.7    0.675  
2.5  1.6  1.5  1.2    1.700  
0.4  0.4  0.4  0.4    0.400 

到目前为止,一切都很好.但是,当我将结果保存到一个csv文件中时,这就是我发现的内容:

So far so good. But when I save the results to a csv file this is what I found:

5.9,5.4,2.4,3.2,4.2250000000000005
0.6,0.7,0.7,0.7,0.6749999999999999
2.5,1.6,1.5,1.2,1.7
0.4,0.4,0.4,0.4,0.4

我想我可以在均值栏中强制使用格式,但是知道为什么会发生这种情况吗?

I guess I can force the format in the mean column, but any idea why this is happenning?

我正在将winpython与python 3.3.2和pandas 0.11.0一起使用

I am using winpython with python 3.3.2 and pandas 0.11.0

推荐答案

您可以使用float_format参数:

import pandas as pd
import io

content = '''\
1    2    3    4   mean 
NaN  NaN  NaN  NaN      NaN  
5.9  5.4  2.4  3.2    4.225  
0.6  0.7  0.7  0.7    0.675  
2.5  1.6  1.5  1.2    1.700  
0.4  0.4  0.4  0.4    0.400'''

df = pd.read_table(io.BytesIO(content), sep='\s+')
df.to_csv('/tmp/test.csv', float_format='%g', index=False)

收益

1,2,3,4,mean
,,,,
5.9,5.4,2.4,3.2,4.225
0.6,0.7,0.7,0.7,0.675
2.5,1.6,1.5,1.2,1.7
0.4,0.4,0.4,0.4,0.4

这篇关于 pandas 错误的十进制计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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