shahid hamdam
shahid hamdam

Reputation: 821

pandas read_csv auto changes decimal places

I am reading a csv file with pandas into dataframe. It is auto changing decimal places.

Before reading csv

enter image description here

After Reading csv with pandas

enter image description here

The code I am using to read is.

df = pd.read_csv(file_path)

Please note I don't know what columns the file can contain as I am getting this file in input so I cannot specify dtype while reading.

Upvotes: 0

Views: 1611

Answers (2)

shahid hamdam
shahid hamdam

Reputation: 821

I got it. read_csv parameter float_precision='round_trip' does the job. It remembers the float precision. Thank you

Upvotes: 2

Neelesh Batham
Neelesh Batham

Reputation: 196

You can't avoid pandas from trying to convert numeric/boolean values in the CSV file. Look at the source code of pandas for the IO parsers, in particular functions _convert_to_ndarrays, and _convert_types. https://github.com/pydata/pandas/blob/master/pandas/io/parsers.py

You can always assign the type you want after you have read the file:

df.phone = df.phone.astype(str)

Upvotes: 0

Related Questions