Paul
Paul

Reputation: 33

Add leading 0 to column of data in data frame

Existing data in data frame has dropped leading zero which is required as a part number. The number stored, needs to be 8 digits long and now they vary based on the removal of the leading 0's

Sorry I am new to Python and this may be built in function in Pandas but I have not found a way to convert this type of formatting. I have over 2000 part numbers to convert over all

EG:

Part No
9069
38661
90705
9070
907
970206

Part number needs to be:

Part No
00009069
00038661
00090705
00009070
00000907
00970206

Upvotes: 0

Views: 60

Answers (1)

Rajat Jain
Rajat Jain

Reputation: 2032

Use astype before using zfill, as follows:

df['Part'].astype(str).str.zfill(8)

Upvotes: 2

Related Questions