mars.dataframe.Series.str.split#
- Series.str.split(pat=None, n=-1, expand=False)#
根据给定的分隔符/定界符分割字符串。
从开头开始,将Series/Index中的字符串按指定的分隔符字符串拆分。等价于
str.split()。- Parameters
- Returns
类型与调用者匹配,除非
expand=True(见注释)。- Return type
另请参阅
Series.str.split根据给定的分隔符/定界符分割字符串。
Series.str.rsplit从右侧开始围绕给定的分隔符/定界符拆分字符串。
Series.str.join使用传递的分隔符连接作为元素包含在Series/Index中的列表。
str.split用于分割的标准库版本。
str.rsplitrsplit的标准库版本。
备注
n关键字的处理取决于找到的划分数量:
如果找到分割数 > n,仅生成前 n 个分割
如果找到的分割小于等于 n,则执行所有分割
如果某一行找到的拆分数量 < n,则在
expand=True的情况下,填充至 n 时附加 None
如果使用
expand=True,Series 和 Index 调用者将分别返回 DataFrame 和 MultiIndex 对象。示例
>>> import numpy as np >>> import mars.dataframe as md >>> s = md.Series(["this is a regular sentence", >>> "https://docs.python.org/3/tutorial/index.html", >>> np.nan]) >>> s.execute() 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html 2 NaN dtype: object
在默认设置中,字符串通过空格分割。
>>> s.str.split().execute() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object
没有n参数时,rsplit和split的输出是相同的。
>>> s.str.rsplit().execute() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object
n 参数可以用来限制分隔符的分割数量。split 和 rsplit 的输出是不同的。
>>> s.str.split(n=2).execute() 0 [this, is, a regular sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object
>>> s.str.rsplit(n=2).execute() 0 [this is a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object
参数 pat 可以用于按其他字符进行分割。
>>> s.str.split(pat = "/").execute() 0 [this is a regular sentence] 1 [https:, , docs.python.org, 3, tutorial, index... 2 NaN dtype: object
当使用
expand=True时,分割的元素将扩展为单独的列。如果存在 NaN,它会在分割过程中在各列之间传播。>>> s.str.split(expand=True).execute() 0 1 2 3 0 this is a regular 1 https://docs.python.org/3/tutorial/index.html None None None 2 NaN NaN NaN NaN \ 4 0 sentence 1 None 2 NaN
对于稍微复杂一些的用例,例如从网址中分离html文档名称,可以使用参数设置的组合。
>>> s.str.rsplit("/", n=1, expand=True).execute() 0 1 0 this is a regular sentence None 1 https://docs.python.org/3/tutorial index.html 2 NaN NaN
记得在显式使用正则表达式时转义特殊字符。
>>> s = pd.Series(["1+1=2"]) >>> s.str.split(r"\+|=", expand=True).execute() 0 1 2 0 1 1 2