pandas python - round() not behaving correctly

2024/10/5 15:07:47

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

Here is the 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

my code:

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

Output

                                            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

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)

output

4.0

FYI - im using python version 2.7.9

Answer

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 npdata = [[ 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.

Try running

df['Våren 2015'] < 3.95

or

df['Våren 2015'] - 3.95

I suspect the display is misleading you.

https://en.xdnf.cn/q/119661.html

Related Q&A

Converting dictionary into string

d={a:Apple,b:ball,c:cat}The above dictionary I have and I want my Output like the below-mentioned resultres="a=Apple,b=ball,c=cat"Is it possible in a pythonic way then please answer it I have…

Django Getting QuerySet.values() based on condition

Lets say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.Lets say I retrieve one parent by calling filter (so as to have a Que…

Condition checking in python with user input [duplicate]

This question already has answers here:If...else statement issue with raw_input on Python(2 answers)Closed 8 years ago.I tried taking an input from keyboard. the checking that input with an if else sta…

Triangle of T in Python

EDIT ** I cant multiply strings by an integer. Its for a homework and those were the instructions **I need to do a triangle in python using for loops or while loops(mandatory). The final output should …

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…

file modifiaction and manipulation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…

how to save python session input and output [duplicate]

This question already has answers here:How to save a Python session, including input and output, as a text?(4 answers)Closed 2 years ago.All of the ways which discussed this question save the history …

flask_mysqldb Delete FROM variable table [duplicate]

This question already has answers here:Python sqlite3 parameterized drop table(1 answer)How do I use SQL parameters with python?(1 answer)Closed 6 years ago.So i use flask_mysqldb in a Flask(Python we…