Reputation: 131
I am trying to read in temperature data from a .csv file using pandas. Here is a sample of the file:
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
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