How to add zeros and limit to 8 digits using Pandas

I have a DataFrame column that's composed of numbers. It's necessary to add zeros to the left if some elements don't reach 8 digits.

ex:

column1
81234567
1294569
23479

It needs to be like:

column1
81234567
01294569
00023479

Upvotes: 0

Views: 408

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195428

You can use .str.zfill:

df["column1"] = df["column1"].astype(str).str.zfill(8)
print(df)

Prints:

    column1
0  81234567
1  01294569
2  00023479

Upvotes: 3

Related Questions