首页 > 解决方案 > 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.

标签: pythonpandasleading-zero

解决方案


Use post processing by str.lstrip:

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

推荐阅读