Skip to content Skip to sidebar Skip to footer

Pandas To_csv Script Modifying Numbers To Long Float Numbers When It Shouldn't Be

The pandas script below keeps modifying my data exported to CSV when it shouldn't be. If you compare the original file to the modified testing2.csv you'll see that numbers like: 0.

Solution 1:

This looks like a precision issue.

Try changing your to_csv lines to include the argument float_format='%.4f' which will round things to 2 decimal places.

Solution 2:

Pandas supports two basic numeric types, Int64 and Float64. Float64 will not represent decimal values exactly, because it is a floating-point type. Your options are

  1. Specify float_format as suggested by @TomAugspurger (this can be done column-wise or for the whole dataframe
  2. Convert your column dtype to object

Option 2 can be done liuke this:

df['col_name'] = df['col_name'].astype(object)

Solution 3:

Try this :)

csvdata = pandas.read_csv('testing.csv', dtype={'TITLE5' : 'object', 'TITLE5.1' : 'object', 'TITLE5.2' : 'object', 'TITLE5.3' : 'object'})

Post a Comment for "Pandas To_csv Script Modifying Numbers To Long Float Numbers When It Shouldn't Be"