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.
- 参数:
- 返回:
返回包含最少字符数的 Series 或 Index 对象。
- 返回类型:
参见
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