str attribute contains most of the methods in string.
df['ID'] = df['ID'].str.zfill(15)
See more: http://pandas.pydata.org/pandas-docs/stable/text.html
Answer from Guangyang Li on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 3.0.5 documentation - PyData |
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a special character and the zero is added to the right of it (str.zfill() would have moved it to the left).
w3resource
w3resource.com › pandas › series › series-str-zfill.php
Pandas Series: str.zfill() function - w3resource
May 20, 2026 - Pandas Series: str.zfill() function: The str.zfill() function is used to pad strings in the Series/Index by prepending '0' characters.
GeeksforGeeks
geeksforgeeks.org › python-pandas-series-str-zfill
Python | Pandas Series.str.zfill() | GeeksforGeeks
September 21, 2018 - In this example, a width of 8 is set and zeroes are prefixed to Salary column using zfill() method. Since the Salary column has data type of int64, it's converted to string first using astype() method. ... # importing pandas import pandas as pd # making data frame from csv at url data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/employees.csv") # converting to string dtype data["Salary"]= data["Salary"].astype(str) # width of output string width = 10 # calling method and overwriting series data["Salary"]= data["Salary"].str.zfill(width) # display data Output: As shown in the output image, zeroes have been prefixed and length of every string in salary column is 10 now.
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 3.0.5 documentation
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a special character and the zero is added to the right of it (str.zfill() would have moved it to the left).
Statology
statology.org › home › how to use zfill() function in pandas
How to Use zfill() Function in Pandas
April 19, 2024 - It’s important to note that this function only works with string variables. If you attempt to use the function with a numeric column, you will receive a TypeError because the function only works with strings. It’s also important to note that this function only works on pandas Series.
Top answer 1 of 8
155
str attribute contains most of the methods in string.
df['ID'] = df['ID'].str.zfill(15)
See more: http://pandas.pydata.org/pandas-docs/stable/text.html
2 of 8
124
Try:
df['ID'] = df['ID'].apply(lambda x: '{0:0>15}'.format(x))
or even
df['ID'] = df['ID'].apply(lambda x: x.zfill(15))
Databricks
api-docs.databricks.com › python › pyspark › latest › pyspark.pandas › api › pyspark.pandas.Series.str.zfill.html
pyspark.pandas.Series.str.zfill — PySpark master documentation
Note that NaN is not a string, therefore it is converted to NaN. The minus sign in ‘-1’ is treated as a regular character and the zero is added to the left of it (str.zfill() would have moved it to the left).
Pandas
pandas.pydata.org › pandas-docs › version › 0.25.0 › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 0.25.0 documentation
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a regular character and the zero is added to the left of it (str.zfill() would have moved it to the left).
Pandas
pandas.pydata.org › pandas-docs › version › 2.0 › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 2.0.3 documentation
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a special character and the zero is added to the right of it (str.zfill() would have moved it to the left).
Skytowner
skytowner.com › explore › adding_leading_zeros_to_strings_of_a_column_in_pandas
Adding leading zeros to strings of a column in Pandas
To add leading zeros to strings of a column in Pandas, use the Series' str.zfill(~) method.
Intelpython
intelpython.github.io › sdc-doc › latest › _api_ref › pandas.Series.str.zfill.html
pandas.Series.str.zfill — Intel® Scalable Dataframe Compiler 0.1 documentation
import pandas as pd from numba import njit @njit def series_str_zfill(): series = pd.Series(['dog', 'foo', 'bar']) # Series of 'dog', 'foo', 'bar' out_series = series.str.zfill(5) return out_series # Expect series of '00dog', '00foo', '00bar' print(series_str_zfill())
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas documentation
Note that 10 and NaN are not strings, therefore they are converted to NaN. The minus sign in '-1' is treated as a special character and the zero is added to the right of it (str.zfill() would have moved it to the left).
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.Series.str.zfill.html
pandas.Series.str.zfill — pandas 0.22.0 documentation
Filling left side of strings in the Series/Index with 0. Equivalent to str.zfill().
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.Series.str.zfill.html
pyspark.pandas.Series.str.zfill — PySpark 4.2.0 documentation
Note that NaN is not a string, therefore it is converted to NaN. The minus sign in ‘-1’ is treated as a regular character and the zero is added to the left of it (str.zfill() would have moved it to the left).
Codepointtech
codepointtech.com › home › mastering data formatting: how to use zfill() function in pandas
Mastering Data Formatting: How to Use zfill() Function in Pandas - codepointtech.com
July 4, 2026 - If the string contains a sign character ... access this function through the .str accessor on a Series. This allows you to apply string methods element-wise across an entire column or Series of string data....
Beautiful Soup
tedboy.github.io › pandas › generated › pandas.Series.str.zfill.html
pandas.Series.str.zfill — Pandas Doc
Filling left side of strings in the Series/Index with 0. Equivalent to str.zfill().