Pandas Series: str.repeat() function
Series-str.repeat() function
The str.repeat() function is used to duplicate each string in the Series or Index.
Syntax:
Series.str.repeat(self, repeats)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
repeats | Same value for all (int) or different value per (sequence). | int or sequence of int | Required |
Returns: Series or Index of object
Series or Index of repeated string objects specified by input parameter repeats.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['x', 'y', 'z'])
s
Output:
0 x 1 y 2 z dtype: object
Example - Single int repeats string in Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['x', 'y', 'z'])
s.str.repeat(repeats=2)
Output:
0 xx 1 yy 2 zz dtype: object
Example - Sequence of int repeats corresponding string in Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['x', 'y', 'z'])
s.str.repeat(repeats=[2, 3, 4])
Output:
0 xx 1 yyy 2 zzzz dtype: object
Previous: Series-str.partition() function
Next: Series-str.replace() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-str-repeat.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics