Reputation: 821
I am reading a csv file with pandas into dataframe. It is auto changing decimal places.
Before reading csv
After Reading csv with pandas
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
Reputation: 821
I got it. read_csv parameter float_precision='round_trip'
does the job. It remembers the float precision. Thank you
Upvotes: 2
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