maxframe.dataframe.Series.str.pad#

Series.str.pad(width: int, side: Literal['left', 'right', 'both'] = 'left', fillchar: str = ' ')#

将 Series/Index 中的字符串填充到指定宽度。

This function pads strings in a Series or Index to a specified width, filling the extra space with a character of your choice. It provides flexibility in positioning the padding, allowing it to be added to the left, right, or both sides. This is useful for formatting strings to align text or ensure consistent string lengths in data processing.

参数:
  • width (int) -- 结果字符串的最小宽度;额外字符将使用 fillchar 中定义的字符进行填充。

  • side ({'left', 'right', 'both'}, default 'left') -- 用于填充结果字符串的一侧。

  • fillchar (str, default ' ') -- 用于填充的额外字符,默认为空格。

返回:

返回包含最少字符数的 Series 或 Index 对象。

返回类型:

Series or Index of object

参见

Series.str.rjust

用任意字符填充字符串的左侧。等效于 Series.str.pad(side='left')

Series.str.ljust

用任意字符填充字符串的右侧。等效于 Series.str.pad(side='right')

Series.str.center

用任意字符填充字符串的两侧。等效于 Series.str.pad(side='both')

Series.str.zfill

通过在字符串前添加 '0' 字符来填充 Series/Index 中的字符串。等效于 Series.str.pad(side='left', fillchar='0')

示例

>>> import maxframe.dataframe as md
>>> s = md.Series(["caribou", "tiger"])
>>> s.execute()
0    caribou
1      tiger
dtype: str
>>> s.str.pad(width=10).execute()
0       caribou
1         tiger
dtype: str
>>> s.str.pad(width=10, side="right", fillchar="-").execute()
0    caribou---
1    tiger-----
dtype: str
>>> s.str.pad(width=10, side="both", fillchar="-").execute()
0    -caribou--
1    --tiger---
dtype: str