pandas.Series.str.slice#

Series.str.slice(start=None, stop=None, step=None)[源代码]#

切分系列或索引中每个元素的子字符串。

参数
start整型,可选

切片操作的开始位置。

stop整型,可选

切片操作的停止位置。

step整型,可选

切片操作的步长。

退货
对象的系列或索引

来自原始字符串对象的切片子字符串的序列或索引。

参见

Series.str.slice_replace

用字符串替换切片。

Series.str.get

将元素返回到相应位置。相当于 Series.str.slice(start=i, stop=i+1) 使用 i 就是这个位置。

示例

>>> s = pd.Series(["koala", "dog", "chameleon"])
>>> s
0        koala
1          dog
2    chameleon
dtype: object
>>> s.str.slice(start=1)
0        oala
1          og
2    hameleon
dtype: object
>>> s.str.slice(start=-1)
0           a
1           g
2           n
dtype: object
>>> s.str.slice(stop=2)
0    ko
1    do
2    ch
dtype: object
>>> s.str.slice(step=2)
0      kaa
1       dg
2    caeen
dtype: object
>>> s.str.slice(start=0, stop=5, step=3)
0    kl
1     d
2    cm
dtype: object

行为等同于:

>>> s.str[0:5:3]
0    kl
1     d
2    cm
dtype: object