0

I'm not sure how to read in csv data using Pandas read_csv without having the data rounded up. Here's the first few rows of the csv (opened in Excel, where the Excel column type is Number)

enter image description here

When I read this CSV into my Jupyter Notebook using Pandas, both columns are being rounded up. I tried using the option float_precision = 'round_trip' and also first reading the columns in as str then converting to float, but once converted to float, the numbers are rounded up again. I made sure that my Jupyter Notebook display precision is greater than two.

How do I read this data in while preserving the precision?

This is what the csv file looks like in Xcode

enter image description here

I first read in the csv with the following code

CPI = pd.read_csv('CPI.csv', dtype={'Year': str, 'Annual_Avg': np.float64 , 'Annual_Percent_Change': np.float64})

Afterwards, the dataframe looks like this: enter image description here

After closing out my kernel and restarting Jupyter, I'm now seeing the decimals displayed again. I don't know why it was initially rounding, it now looks ok. Should I now delete this post? Not sure what the stackoverflow protocol is if you've 'solved' your own question.

sjc725
  • 491
  • 2
  • 5
  • 14
  • Can you try to include `dtype : {‘column_name2’: np.float64,‘column_name2’: np.float64}` inside the `read_csv` method & try? – moys Oct 13 '19 at 05:10
  • Could you show some rows of the `csv` file itself? – hpaulj Oct 13 '19 at 06:41
  • @moys When I add the dtype to read the columns as np.float64, the numbers are still rounded up. hpaulj, I posted a few rows from the CSV in the edit of my question. – sjc725 Oct 13 '19 at 21:46
  • So is it an Excel file or a CSV file? They're different. CSV files are plain text. Excel files (which end with xlsx) are not. You need to use read_csv or read_excel depending on what kind of file you want pandas to read. – ba_ul Oct 13 '19 at 21:48
  • @ba_ul, I should have been more clear. I saved it as a CSV within Excel, so it's a CSV, and I'm using read_csv. – sjc725 Oct 13 '19 at 22:13
  • OK. Can you upload a part of the file or the whole file so that we can take a look and see what happens when we load the file using read_csv? – ba_ul Oct 13 '19 at 22:56
  • can you post the code you are trying to run? – moys Oct 14 '19 at 01:40
  • @moys, here is the code to read in the file: `CPI = pd.read_csv('CPI.csv', dtype={'Year': str, 'Annual_Avg': np.float64 , 'Annual_Percent_Change': np.float64})` – sjc725 Oct 14 '19 at 21:46

0 Answers0