- API reference
- Series
- pandas.Serie...
pandas.Series.str.slice_replace#
- Series.str.slice_replace(start=None,stop=None,repl=None)[source]#
Replace a positional slice of a string with another value.
- Parameters:
- startint, optional
Left index position to use for the slice. If not specified (None),the slice is unbounded on the left, i.e. slice from the startof the string.
- stopint, optional
Right index position to use for the slice. If not specified (None),the slice is unbounded on the right, i.e. slice until theend of the string.
- replstr, optional
String for replacement. If not specified (None), the sliced regionis replaced with an empty string.
- Returns:
- Series or Index
Same type as the original object.
See also
Series.str.sliceJust slicing without replacement.
Examples
>>>s=pd.Series(['a','ab','abc','abdc','abcde'])>>>s0 a1 ab2 abc3 abdc4 abcdedtype: object
Specify juststart, meaning replacestart until the end of thestring withrepl.
>>>s.str.slice_replace(1,repl='X')0 aX1 aX2 aX3 aX4 aXdtype: object
Specify juststop, meaning the start of the string tostop is replacedwithrepl, and the rest of the string is included.
>>>s.str.slice_replace(stop=2,repl='X')0 X1 X2 Xc3 Xdc4 Xcdedtype: object
Specifystart andstop, meaning the slice fromstart tostop isreplaced withrepl. Everything before or afterstart andstop isincluded as is.
>>>s.str.slice_replace(start=1,stop=3,repl='X')0 aX1 aX2 aX3 aXc4 aXdedtype: object