pandas python - round() 行为不正确 [英] pandas python - round() not behaving correctly

查看:152
本文介绍了pandas python - round() 行为不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据框中的值四舍五入到小数点后 1 位.

I'm rounding values in a dataframe to 1 decimal place.

这里是df

                                            Våren 2015  Hösten 2014  Våren 2014
Question                                                                      
1) Maten är vällagad och smakar bra          4.000000     3.469136    3.678571
Δ 2) Maten ser aptitlig ut                   3.883721     3.296296    3.592593
3) Det är en bra variation på grönsakerna    3.365854     2.901235    3.333333
Δ 4) Maten är bra varierad och passar mig    3.725000     3.365854    3.607143
5) Portionsstorleken är lagom                4.166667     3.875000    4.071429
Δ 6) Konsistensen på maten är bra            4.000000     3.468354    3.607143
7) Info om matens innehåll är tydlig         3.950000     3.454545    3.821429
8) Maten levereras i en bra förpackning      3.880952     3.987179    4.214286
9) Jag får den mat jag har beställt          4.166667     4.194805    4.481481

我的代码:

df.applymap(lambda x: round(x,1))

输出

                                            Våren 2015  Hösten 2014  Våren 2014
Question                                                                      
1) Maten är vällagad och smakar bra               4.0          3.5         3.7
Δ 2) Maten ser aptitlig ut                        3.9          3.3         3.6
3) Det är en bra variation på grönsakerna         3.4          2.9         3.3
Δ 4) Maten är bra varierad och passar mig         3.7          3.4         3.6
5) Portionsstorleken är lagom                     4.2          3.9         4.1
Δ 6) Konsistensen på maten är bra                 4.0          3.5         3.6
7) Info om matens innehåll är tydlig              3.9          3.5         3.8
8) Maten levereras i en bra förpackning           3.9          4.0         4.2
9) Jag får den mat jag har beställt               4.2          4.2         4.5

上面的代码错误地将Varen 2015"列中的3.95"四舍五入为 3.9 而不是 4.0.

Code above incorrectly rounds '3.95' in column 'Varen 2015' to 3.9 instead of 4.0.

注意:如果我像这样直接将数字插入到函数中,它会返回正确的值...

Note: If I insert the number directly to the function like so, it returns the correct value...

round(3.95,1)

输出

4.0

仅供参考 - 我使用的是 python 版本 2.7.9

FYI - im using python version 2.7.9

推荐答案

这有点难回答,因为你列出的不是 DataFrame,不是 Python 列表列表等.

It's a bit hard to answer, as what you list is not a DataFrame, not a Python list of lists, etc.

但是,您应该注意,可能没有理由在循环中执行此操作,因为它可以通过矢量方式(并且正确地)完成:

However, you should note that there is probably no reason to do this in a loop, as it can be done vectorially (and correctly):

import numpy as np

data = [[ 4., 3.4691358, 3.67857143],
    [ 3.88372093, 3.2962963, 3.59259259],
    [ 3.36585366, 2.90123457, 3.33333333],
    [ 3.725, 3.36585366, 3.60714286],
    [ 4.16666667, 3.875, 4.07142857],
    [ 4., 3.46835443, 3.60714286],
    [ 3.95, 3.45454545, 3.82142857],
    [ 3.88095238, 3.98717949, 4.21428571],
    [ 4.16666667, 4.19480519, 4.48148148]]

>> np.array(data).round(1)
array([[ 4. ,  3.5,  3.7],
   [ 3.9,  3.3,  3.6],
   [ 3.4,  2.9,  3.3],
   [ 3.7,  3.4,  3.6],
   [ 4.2,  3.9,  4.1],
   [ 4. ,  3.5,  3.6],
   [ 4. ,  3.5,  3.8],
   [ 3.9,  4. ,  4.2],
   [ 4.2,  4.2,  4.5]])

<小时>

编辑 更新您的问题后,我怀疑还有其他问题.许多浮点数不能真正以有限的小数位数显示.


Edit Following the update to your question, I suspect something else. Many floating point numbers cannot really be displayed in a finite number of decimals.

尝试运行

df['Våren 2015'] < 3.95

df['Våren 2015'] - 3.95

我怀疑该显示误导了您.

I suspect the display is misleading you.

这篇关于pandas python - round() 行为不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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