pandas.Series.str.slice#
- Series.str.slice(start=None, stop=None, step=None)[源代码]#
从 Series 或 Index 中的每个元素切片子字符串。
- Parameters:
- startint, optional
切片操作的起始位置。
- stopint, optional
切片操作的停止位置。
- stepint, optional
切片操作的步长。
- Returns:
- Series 或 Index 对象
来自原始字符串对象的切片子字符串的 Series 或 Index。
参见
Series.str.slice_replace用字符串替换切片。
Series.str.get返回指定位置的元素。等同于 Series.str.slice(start=i, stop=i+1),其中 i 为位置。
Examples
>>> 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