Leading zeros in an int column gets removed when converted to string – python

Question:

The title here basically explains my issue/ask, I have an int column with more than 12 digits that starts with leading zeros. my problem is that when i convert these values to string some values lose their leading zeros which are essential for analysis purposes.

https://i.stack.imgur.com/f5Yrj.png
[This is the column, as saved and extracted from a database.

I want to convert these values as is starting with zeros. I only use .astype(str) for the type conversion: (df[‘trxn’] = df[‘trxn’].astype(str). Any advice?

the idea is that after analyzing the dataset we will be uploading those trxn id/numbers into a system to match with the records we have in our database, so it must contain those leading zeros as saved in the database to get the matched records.

Here is an example of how it originally looks like, in DB and csv file:
|Trxn|
|———————|
|009907023031022180158|

This is how it’s returned in python:
|Trxn|
|———————|
| 9907023031022180158 |

Asked By: Shrouq

||

Answers:

It’s not python. It’s just how integers work. Leading zeroes have no mathematical significance.

You can use some built in string methods to pad a string to a particular length filling with an arbitrary character like so:

>>> x = "000002"                        
>>> print x
2
>>> x =  str(x).rjust(6,'0')  #adds 5 leading zeroes to x
>>> print x
000002

Or you can use .zfill()

number= '2' 
print number.zfill(5) #adds 4 leading zeroes to the string
>>> 00002
Answered By: Iha Agrawal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.