Python | Панды Series.str.center ()
Series.str can be used to access the values of the series as strings and apply several methods to it. Pandas Series.str.center() function is used for filling left and right side of strings in the Series/Index with an additional character. The function is equivalent to Python’s str.center().
Syntax: Series.str.center(width, fillchar=’ ‘)
Parameter :
width : Minimum width of resulting string; additional characters will be filled with fillchar
fillchar : Additional character for filling, default is whitespaceReturns : filled
Example #1: Use Series.str.center() function to fill the left and right side of the string in the underlying data of the given series object by ‘*’ symbol.
Выход :

Now we will use Series.str.center() function to fill ‘*’ symbol in the left and right side of the string.
# fill "*" in the left and right side of stringresult = sr.str.center(width = 13, fillchar = "*") # print the resultprint(result) |
Выход :

As we can see in the output, the Series.str.center() function has successfully filled ‘*’ symbol in the left and the right side of the string in the underlying data of the given series object.
Example #2 : Use Series.str.center() function to fill the left and right side of the string in the underlying data of the given series object by ‘*’ symbol.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series(["Mike", "Alessa", "Nick", "Kim", "Britney"]) # Creating the indexidx = ["Name 1", "Name 2", "Name 3", "Name 4", "Name 5"] # set the indexsr.index = idx # Print the seriesprint(sr) |
Выход :

Now we will use Series.str.center() function to fill ‘*’ symbol in the left and right side of the string.
# fill "*" in the left and right side of string# width after filling should be 5result = sr.str.center(width = 5, fillchar = "*") # print the resultprint(result) |
Выход :

As we can see in the output, the Series.str.center() function has successfully filled ‘*’ symbol in the left and the right side of the string in the underlying data of the given series object.
Примечание. Если значение ширины меньше длины фактической строки, то печатается вся строка без ее усечения.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.