Skip to content
Advertisement

An addition of non-existing **trailing** zeros at importing csv file by pandas

Importing a .csv file given by

a
1.4
1.12

leads to an addition of a trailing zero in the first line. How to avoid it?

enter image description here

Advertisement

Answer

This is just the visual representation of the float data in your df. If you want it different, change the format string for it.

For your numbers '{:,g}' would work (see format specification mini language – scroll down for the table that explains it).

Example:

import pandas as pd
import numpy as np

with open("data.txt","w") as f:
    f.write("an1.4n1.12")

df = pd.read_csv("data.txt")
print(df) 

# change the way floats are formatted
pd.options.display.float_format = '{:,.06f}'.format

# change the way floats are formatted
pd.options.display.float_format = '{:,g}'.format

df = pd.read_csv("data.txt")
print(df) 

Output:

# default formatting
      a
0  1.40
1  1.12


# {:,06f}
          a
0 1.400000
1 1.120000

# {:,g}
      a
0   1.4
1  1.12

From a numerical standpoint there is no difference between 1.4 and 1.40 or 00001.40000000000.


Guarav Kumar’s suggestion to use

df = pd.read_csv('minExamp.csv' , dtype = str)

is bad advice – it changes the data type for the (single) column to be of type string.

This will prevent you from calculating anything with the numbers of your dataframe.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement