0

I'm using pandas to read an Excel file which contains earthquake data from the last 10 years. However, the numbers (more specifically the decimal part) seems to change when I read into Python.

The first line has the column "EQ_PRIMARY" = 9.1 in Excel. When I try to output it in Python, I get 9.0999999999999996. This is not a big deal, but I really wanted it to output correctly without using formatting or something like that. What could be the problem?

This is how I'm reading/analyzing the dataset now:

import pandas as pd
data = pd.read_excel("Earthquakes.xls",sheet_name="earthquakes")
data["EQ_PRIMARY"].max()

And the output is

9.0999999999999996

Caio Reis
  • 29
  • 1
  • 6

2 Answers2

0

Edit: I was wrong and I'm sorry. Don't read the rest of my comment.


Pandas doesn't change the number. Your code output correctly returns the number in your excel file. You can check the actual number by clicking the following button in the home tab in MS Excel decimal point change Use the round method if you want to get your number rounded up.

Whichmann
  • 153
  • 1
  • 7
  • Excel is not rounding the number off. I checked and it is 9.1 exactly – Caio Reis Mar 24 '19 at 20:50
  • did you figure this out? This is happening for me too. I have an auxillary list of a mix of numbers some integers, some with a decimal point. For some reason those that should be "9" are coming out "9.0" when I need them to be "9". Also I can't simply change them with the truncate number button as suggested above because then when I do have a decimal "9.2", it comes out "9". – Dan Aug 12 '20 at 13:40
0

I think this is a duplicate question, please see question below:

Pandas read csv file with float values results in weird rounding and decimal digits

The solution was using float_precision='round_trip':- pd.read_csv(source_file, float_precision='round_trip')

half of a glazier
  • 1,864
  • 2
  • 15
  • 45
behold
  • 538
  • 5
  • 19
  • 4
    The question is about reading Excel file, not CSV. This answer is not supported by pandas version 1.1.3. – srodriguex Nov 08 '20 at 03:54
  • 2
    I don't understand how this is the accepted answer. The question relates to `read_excel` and the answer is specific to `read_csv` – Carl H Dec 14 '20 at 11:00