📜  Python| Pandas Series.str.wrap()(1)

📅  最后修改于: 2023-12-03 15:19:15.930000             🧑  作者: Mango

Python | Pandas Series.str.wrap()

Pandas Series.str.wrap()方法用于将文本字符串分成多行,并将所有行标准化为给定的宽度。

语法
Series.str.wrap(width, **kwargs)
参数
  • width(int): 每行的最大宽度。默认为 72。
  • kwargs(dict): 其他可选参数,可以是 expand_tabsreplace_whitespacedrop_whitespacebreak_long_words

其中 expand_tabs= True 将把制表符扩展为空格, replace_whitespace=True 将所有人工空格字符替换为单个空格, drop_whitespace=True 将删除所有空格字符,break_long_words=True 将强制将长单词分成多行,而不考虑宽度。

返回值

返回一个新的 Series,其中每个元素都是被分成多行,并将所有行标准化为给定宽度后的原始字符串。

示例
import pandas as pd
 
# 示例1:将文本字符串中的所有行标准化为给定宽度
s = pd.Series(['This is pandas Series.str.wrap() example', 'In this example we will see how', 
              'to wrap long text into smaller length', 'and convert them into pandas dataframe.'])
 
print("Original Series:")
print(s)
 
# 将所有行标准化为给定宽度80
wrapped = s.str.wrap(width=80)

print("\nWrapped Series:")
print(wrapped)

输出结果:

Original Series:
0         This is pandas Series.str.wrap() example
1            In this example we will see how
2        to wrap long text into smaller length
3    and convert them into pandas dataframe.
dtype: object

Wrapped Series:
0         This is pandas Series.str.wrap() example
1           In this example we will see how to wrap long text into smaller length
2                                        and convert them into pandas dataframe.
dtype: object
# 示例2:使用可选参数 expand_tabs 和 break_long_words
s = pd.Series(['These are examples of expand_tabs and break_long_words.',
               'You can also truncate long lines with max_width as shown below',
               'In this example we are using replace_whitespace=True and adding custom prefix and suffix to each line'])
 
print("Original Series:")
print(s)
 
# 使用 expand_tabs 和 break_long_words 参数
wrapped = s.str.wrap(width=40, expand_tabs=True, break_long_words=True)

print("\nWrapped Series:")
print(wrapped)

输出结果:

Original Series:
0       These are examples of expand_tabs and break_long_words.
1    You can also truncate long lines with max_width as shown below
2    In this example we are using replace_whitespace=True and adding custom prefix and suffix to each line
dtype: object

Wrapped Series:
0                  These are examples of 
                 expand_tabs and 
                 break_long_words.
1    You can also truncate long lines 
                 with max_width as shown 
                 below
2       In this example we are using 
                 replace_whitespace=True 
               and adding custom prefix 
                       and suffix to each 
                                      line
dtype: object
# 示例3:使用可选参数 replace_whitespace 和 drop_whitespace
s = pd.Series(['This is examples with multiple spaces', 'This is example     with       more than      one    space'])
 
print("Original Series:")
print(s)
 
# 使用 replace_whitespace 和 drop_whitespace 参数
wrapped = s.str.wrap(width=30, replace_whitespace=True, drop_whitespace=True)

print("\nWrapped Series:")
print(wrapped)

输出结果:

Original Series:
0                    This is examples with multiple spaces
1    This is example     with       more than      one    space
dtype: object

Wrapped Series:
0         This is examples with
               multiple spaces
1    This is example with more
       than one space
dtype: object

在示例3中,我们可以看到空格被替换并且最终的行不包含空格字符。