0

I have this code that removes a column from a csv file. It works, but when it saves it adds decimal places (seemingly at random) to some of my values in other columns. For example, when I run the code, the value in another column changes from the original 1.815 to 1.8159999999999998, or 1.783 to 1.7830000000000001. Is there a way for me to prevent this from happening? I don't understand why it changes some values in other columns if I'm just removing column [0]. Thanks!

import pandas as pd
import os
import glob

def main():
    path = os.getcwd()
    csv_files = glob.glob(os.path.join(path, "*.csv"))
    for f in csv_files:
        df = pd.read_csv(f)
        first_column = df.columns[0]
        df = df.drop([first_column], axis=1)
        df.to_csv(f, index=False)
main()
  • 1
    Does this answer your question? [float64 with pandas to\_csv](https://stackoverflow.com/questions/12877189/float64-with-pandas-to-csv) – AlexK May 21 '21 at 20:23

1 Answers1

0

That's how computers work! They cannot exactly represent floating point numbers.

https://docs.python.org/3.9/tutorial/floatingpoint.html

You can round them again when writing back to your file using float_format

df.to_csv(f, index=False, float_format="%.f3")

Or use "%.g3" to remove trailing zeroes.

Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30