How to add leading zeros to strings in Pandas?

In Pandas, leading zeros can be added to strings by using the str.zfill() method. This method adds zeros to the beginning of the string until the desired length is reached. This can be useful for formatting data in a consistent way. For example, if the desired string length is 6, the string “1” would be converted to “000001”.


You can use the following syntax to add leading zeros to strings in a pandas DataFrame:

df['ID'] = df['ID'].apply('{:0>7}'.format)

This particular formula adds as many leading zeros as necessary to the strings in the column titled ‘ID’ until each string has a length of 7.

Feel free to replace the 7 with another value to add a different number of leading zeros.

The following example shows how to use this syntax in practice.

Example: Add Leading Zeros to Strings in Pandas

Suppose we have the following pandas DataFrame that contains information about sales and refunds for various stores:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'ID': ['A25', 'B300', 'C6', 'D447289', 'E416', 'F19'],
                   'sales': [18, 12, 27, 30, 45, 23],
                   'refunds': [1, 3, 3, 2, 5, 0]})

#view DataFrame
print(df)

        ID  sales  refunds
0      A25     18        1
1     B300     12        3
2       C6     27        3
3  D447289     30        2
4     E416     45        5
5      F19     23        0

Notice that the length of the strings in the ‘ID’ column are not all equal.

However, we can see that the longest string is 7 characters.

We can use the following syntax to add leading zeros to the strings in the ‘ID’ column so that each string has a length of 7:

#add leading zeros to 'ID' column
df['ID'] = df['ID'].apply('{:0>7}'.format)

#view updated DataFrame
print(df)

        ID  sales  refunds
0  0000A25     18        1
1  000B300     12        3
2  00000C6     27        3
3  D447289     30        2
4  000E416     45        5
5  0000F19     23        0

Notice that leading zeros have been added to the strings in the ‘ID’ column so that each string now has the same length.

Note: You can find the complete documentation for the apply function in pandas .

x