Home > Mobile >  keep exact data format when using pandas read_csv
keep exact data format when using pandas read_csv

Time:02-01

I am reading csv file with pd.read_csv containing any kind of number format. All is fine, except numbers with scientific notation like -6.15000000000001E-02 are converted to float like -0.0615000000000001. Unfortunately, I need to keep the same format as in the original csv file, even the 'E' has to be kept capital. Hope the request is clear and somebody can find a solution. Thanks

CodePudding user response:

Use dtype argument. From pd.read_csv docs:

dtypeType name or dict of column -> type, optional Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32, ‘c’: ‘Int64’} Use str or object together with suitable na_values settings to preserve and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.

test.csv:

a,b
1,-6.15000000000001E-02
print(pd.read_csv('test.csv', dtype={'b': str}))

This outputs

   a                      b
0  1  -6.15000000000001E-02

Just keep in mind that the b column is now a string and not a number. It will need to be explicitly converted before any attempt of doing any computation with it,

CodePudding user response:

Use dtype = str in your import could work:

df = pd.read_csv('filename', dtype = str)
  •  Tags:  
  • Related