MUD
MUD

Reputation: 131

Using pandas to read from .csv file but it's cutting off the decimal places?

I am trying to read in temperature data from a .csv file using pandas. Here is a sample of the file:

enter image description here

My issue is that my code is not extracting the data to 1 decimal place? This is what I have at the moment:

# Import packages
import pandas as pd

# Open  data file
data_file_name = "data_set.csv"
data_file = pd.read_csv(data_file_name, header=2).astype(int)

# Extract temperature data
target_data = data_file["Temperature"].astype(float)
print(target_data.loc[[0]])

After adding in the print statement to see if the first value is -23.5 as it should be, instead I get:

-23.0

Why isn't my code reading the data as a float with 1 d.p?

Upvotes: 1

Views: 438

Answers (1)

Will Osbourne
Will Osbourne

Reputation: 46

I believe your issue is that you're reading in the datafile as .astype(int), which is converting everything in the CSV to an int, so you are unable to recover the decimal by doing .astype(float). Try to not specify type on the inital read_csv, as Pandas can normally handle properly typing automatically.

Upvotes: 3

Related Questions