Table of Contents
Adding leading zeros to strings in Pandas can be achieved by using the “zfill()” function, which adds zeros in front of the string to make it a certain length. This function can be applied to a specific column in a Pandas dataframe or to a single string. It is useful for formatting data, such as adding leading zeros to numerical data that needs to be sorted or compared as strings. This method can be especially useful when working with data that contains codes or identifiers that require a specific length, such as customer IDs or product codes. By using the “zfill()” function, users can easily manipulate and format their data in Pandas without the need for manual editing.
Add Leading Zeros to Strings in Pandas
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 .
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I add leading zeros to strings in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-leading-zeros-to-strings-in-pandas/
stats writer. "How can I add leading zeros to strings in Pandas?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-leading-zeros-to-strings-in-pandas/.
stats writer. "How can I add leading zeros to strings in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-leading-zeros-to-strings-in-pandas/.
stats writer (2024) 'How can I add leading zeros to strings in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-leading-zeros-to-strings-in-pandas/.
[1] stats writer, "How can I add leading zeros to strings in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add leading zeros to strings in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
