Jun Jang Jun Jang - 6 months ago 32
Python Question

Leading zero issues with `pd.read_csv` (Pandas)

I have a column of values such as this:

123, 234, 345, 456, 567

When I do

pd.read_csv(dtype = {'column': str})


pd.read_csv(dtype = 'column': object})

they both produce values like

00123, 00234, 00345, 00456, 00567.

I was searching through stackexchange, and people say that you should use
dtype: object
, but it doesn't work for me..

Answer Source

If you want to read in your data as integers, drop the dtype:

df = pd.read_csv('data.csv')

If you want to convert the fields to strings, you can apply a str transformation with df.astype:

df = pd.read_csv('data.csv').astype(str)

Another option would be to use a converter:

df = pd.read_csv('data.csv', converters={'ColName': str})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download