Python | Pandas Series.str.repeat()

27 Oct, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas str.repeat() method is used to repeat string values in the same position of passed series itself. An array can also be passed in case to define the number of times each element should be repeated in series. For that case, length of array must be same as length of Series. .str has to be prefixed everytime before calling this function as it’s a string method and also to differentiate it from the python’s default repeat method. 
Syntax: Series.str.repeat(repeats)Parameters: repeats: int or List of int to define number of times string has to be repeated. (Size of list must be equal to series)Return type: Series with repeated values  
To download the CSV used in code, click here.In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.  
Example #1: Repeating same number of timesIn this example, a single integer is passed as repeats parameter and hence every string value in the series will be repeated same number of times. Before applying any operation, null values must be removed to avoid errors. Hence dropna() method is used to remove null values. 
Python3
# importing pandas moduleimport pandas as pd # making data framedata = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # removing null values to avoid errorsdata.dropna(how ='all', inplace = True) # overwriting with repeated valuedata["Team"]= data["Team"].str.repeat(2) # displaydata
Output: As shown in the output image, every string in the series was repeated twice.  
  Example #2: Different values for each stringIn this example, a sample data frame of 10 rows is created using .head() method. After that a list of 10 integers is created and passed to the repeat() function to repeat each string different number of times. 
Python3
# importing pandas moduleimport pandas as pd # making data framedata = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") # removing null values to avoid errorsdata.dropna(how ='all', inplace = True) # creating data of 10 rowssample_data = data.head(10).copy() # creating list of 10 intrepeat_list =[2, 1, 3, 4, 1, 5, 0, 6, 1, 2] # calling repeat functionsample_data["Name"]= sample_data["Name"].str.repeat(repeat_list) # displaying datasample_data
Output: As shown in the output image, the string is repeated according to the integer present at the same index in the repeat_list. Note: One of the values in list is set to 0 and hence the string was repeated 0 times in final series (The older string value also got deleted and stored blank instead)  
 
ruhelaa48
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Convert integer to string in Python
Python | os.path.join() method
Create a Pandas DataFrame from Lists