用法:
Series.str.split(pat=None, n=- 1, expand=False, *, regex=None)围绕给定的分隔符/分隔符拆分字符串。
在指定的分隔符字符串处从头开始拆分系列/索引中的字符串。
- pat:str 或编译的正则表达式,可选
要拆分的字符串或正则表达式。如果未指定,则在空格处拆分。
- n:int,默认 -1(全部)
限制输出中的拆分数量。
None, 0 和 -1 将被解释为返回所有拆分。- expand:布尔值,默认为 False
将拆分的字符串展开为单独的列。
如果
True,返回 DataFrame/MultiIndex 扩展维度。如果
False,则返回包含字符串列表的系列/索引。
- regex:布尔值,默认无
确定 passed-in 模式是否为正则表达式:
如果
True,假设 passed-in 模式是正则表达式如果
False,则将模式视为文字字符串。如果
None和pat长度为 1,则将pat视为文字字符串。如果
None和pat长度不为 1,则将pat视为正则表达式。如果
pat是已编译的正则表达式,则不能设置为 False
- Series、Index、DataFrame 或 MultiIndex
类型匹配调用者,除非
expand=True(见注释)。
- ValueError
如果
regex为 False 并且pat是已编译的正则表达式
参数:
返回:
抛出:
注意:
n关键字的处理取决于找到的拆分数量:如果发现拆分 >
n,请先进行n拆分如果发现拆分 n ,则进行所有拆分
如果对于某一行,找到的拆分数 n ,则追加
None以填充到nifexpand=True
如果使用
expand=True,Series 和 Index 调用者分别返回 DataFrame 和 MultiIndex 对象。使用带有
pat的regex=False作为编译的正则表达式会引发错误。例子:
>>> s = pd.Series( ... [ ... "this is a regular sentence", ... "https://docs.python.org/3/tutorial/index.html", ... np.nan ... ] ... ) >>> s 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html 2 NaN dtype:object在默认设置中,字符串由空格分隔。
>>> s.str.split() 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() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype:objectn参数可用于限制分隔符上的拆分数量。split和rsplit的输出不同。>>> s.str.split(n=2) 0 [this, is, a regular sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype:object>>> s.str.rsplit(n=2) 0 [this is a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype:objectpat参数可用于按其他字符分割。>>> s.str.split(pat="/") 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) 0 1 2 3 4 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html None None None None 2 NaN NaN NaN NaN NaN对于稍微复杂的用例,例如从 url 中拆分 html 文档名称,可以使用参数设置的组合。
>>> s.str.rsplit("/", n=1, expand=True) 0 1 0 this is a regular sentence None 1 https://docs.python.org/3/tutorial index.html 2 NaN NaN请记住在显式使用正则表达式时转义特殊字符。
>>> s = pd.Series(["foo and bar plus baz"]) >>> s.str.split(r"and|plus", expand=True) 0 1 2 0 foo bar baz正则表达式可用于处理 url 或文件名。当
pat是字符串且regex=None(默认值)时,仅当len(pat) != 1时,给定的pat才会编译为正则表达式。>>> s = pd.Series(['foojpgbar.jpg']) >>> s.str.split(r".", expand=True) 0 1 0 foojpgbar jpg>>> s.str.split(r"\.jpg", expand=True) 0 1 0 foojpgbar当
regex=True,pat被解释为正则表达式>>> s.str.split(r"\.jpg", regex=True, expand=True) 0 1 0 foojpgbar编译的正则表达式可以传递为
pat>>> import re >>> s.str.split(re.compile(r"\.jpg"), expand=True) 0 1 0 foojpgbar当
regex=False,pat被解释为字符串本身>>> s.str.split(r"\.jpg", regex=False, expand=True) 0 0 foojpgbar.jpg
相关用法
- Python pandas.Series.str.startswith用法及代码示例
- Python pandas.Series.str.strip用法及代码示例
- Python pandas.Series.str.swapcase用法及代码示例
- Python pandas.Series.str.slice用法及代码示例
- Python pandas.Series.str.slice_replace用法及代码示例
- Python pandas.Series.str.isdecimal用法及代码示例
- Python pandas.Series.str.get用法及代码示例
- Python pandas.Series.str.replace用法及代码示例
- Python pandas.Series.str.endswith用法及代码示例
- Python pandas.Series.str.isspace用法及代码示例
- Python pandas.Series.str.isdigit用法及代码示例
- Python pandas.Series.str.wrap用法及代码示例
- Python pandas.Series.str.isalnum用法及代码示例
- Python pandas.Series.str.zfill用法及代码示例
- Python pandas.Series.str.partition用法及代码示例
- Python pandas.Series.str.isnumeric用法及代码示例
- Python pandas.Series.str.count用法及代码示例
- Python pandas.Series.str.rpartition用法及代码示例
- Python pandas.Series.str.removesuffix用法及代码示例
- Python pandas.Series.str.rsplit用法及代码示例
注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Series.str.split。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
