Python | Панды Series.rename ()
Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.
Pandas Series.rename() function is used to alter Series index labels or name for the given Series object.
Syntax: Series.rename(index=None, **kwargs)
Parameter :
index : dict-like or functions are transformations to apply to the index
copy : Also copy underlying data
inplace : Whether to return a new Series. If True then value of copy is ignored.
level : In case of a MultiIndex, only rename labels in the specified level.Returns : Series, DataFrame, or None
Example #1: Use Series.rename() function to rename the name of the given Series object.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series([10, 25, 3, 11, 24, 6]) # Create the Indexindex_ = ["Coca Cola", "Sprite", "Coke", "Fanta", "Dew", "ThumbsUp"] # set the indexsr.index = index_ # Print the seriesprint(sr) |
Выход :
Now we will use Series.rename() function to rename the name of the given series object.
# rename the seriesresult = sr.rename("Beverages") # Print the resultprint(result) |
Выход :

As we can see in the output, the Series.rename() function has successfully renamed the given series object.
Example #2: Use Series.rename() function to rename the MultiIndex axis of the given Series object.
# importing pandas as pdimport pandas as pd # Creating the Seriessr = pd.Series(["New York", "Chicago", "Toronto", "Lisbon", "Rio"]) # Create the MultiIndexindex_ = pd.MultiIndex.from_product([["Names"], ["City 1", "City 2", "City 3", "City 4", "City 5"]], names =["Level 1", "Level 2"]) # set the indexsr.index = index_ # Print the seriesprint(sr) |
Выход :

Now we will use Series.rename() function to rename the 0th level of the given series object.
# rename the 0th levelresult = sr.rename(level = 0, index = "Row_axis") # Print the resultprint(result) |
Выход :

As we can see in the output, the Series.rename() function has successfully renamed the 0th level of the given series object.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.