pandas.Series.str.wrap#

Series.str.wrap(width, **kwargs)[源代码]#

在指定的行宽处换行 Series/Index 中的字符串。

此方法具有与 textwrap.TextWrapper 相同的关键字参数和默认值。

Parameters:
widthint

最大行宽。

expand_tabsbool, optional

如果为 True,则将制表符扩展为空格(默认值:True)。

replace_whitespacebool, optional

如果为 True,则将制表符扩展后剩余的每个空格字符(由 string.whitespace 定义)替换为单个空格(默认值:True)。

drop_whitespacebool, optional

如果为 True,则在换行后出现在行首或行尾的空格将被删除(默认值:True)。

break_long_wordsbool, optional

如果为 True,那么长于 width 的单词将被拆分,以确保没有行的长度超过 width。如果为 False,则长单词不会被拆分,某些行的长度可能会超过 width(默认值:True)。

break_on_hyphensbool, optional

如果为 True,则优先在空格处和复合词的连字符后面换行,这符合英语的习惯。如果为 False,则仅将空格视为潜在的换行点,但如果希望单词真正不可分割,则需要将 break_long_words 设置为 False(默认为 True)。

Returns:
Series 或 Index

Notes

在内部,此方法使用一个 textwrap.TextWrapper 实例,并带默认设置。要获得与 R 语言的 stringr 库的 str_wrap 函数相匹配的行为,请使用以下参数:

  • expand_tabs = False

  • replace_whitespace = True

  • drop_whitespace = True

  • break_long_words = False

  • break_on_hyphens = False

Examples

>>> s = pd.Series(['line to be wrapped', 'another line to be wrapped'])
>>> s.str.wrap(12)
0             line to be\nwrapped
1    another line\nto be\nwrapped
dtype: object