Search code examples
pythonpandasleading-zero

Remove leading zeros when reading a csv file


I have a CSV file that looks something like this -

    Location ID      Location Name
        3543459      A
         20541       B
          C320       C
           ...       ..

When I read the file using pd.read_csv, I get something like this -

Location ID      Location Name
   03543459      A
   0020541       B
   000C320       C
       ...       ..

How to avoid leading zeros? I did some research, all the questions I could ifind were based on producing the leading zeros in the df.


Solution

  • Use post processing by str.lstrip:

    df['Location ID'] = df['Location ID'].str.lstrip('0')