Series.str.zfill(self, width)
[source]
Pad strings in the Series/Index by prepending ‘0’ characters.
Strings in the Series/Index are padded with ‘0’ characters on the left of the string to reach a total string length width
. Strings in the Series/Index with length greater or equal to width
are unchanged.
Parameters: |
|
---|---|
Returns: |
|
See also
Series.str.rjust
Series.str.ljust
Series.str.pad
Series.str.center
Differs from str.zfill()
which has special handling for ‘+’/’-‘ in the string.
>>> s = pd.Series(['-1', '1', '1000', 10, np.nan]) >>> s 0 -1 1 1 2 1000 3 10 4 NaN dtype: object
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). 1000
remains unchanged as it is longer than width
.
>>> s.str.zfill(3) 0 0-1 1 001 2 1000 3 NaN 4 NaN dtype: object
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.Series.str.zfill.html