Python | Панды MultiIndex.reorder_levels ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas MultiIndex.reorder_levels() function is used to rearrange levels using input order. It may not drop or duplicate levels. The function take list as an input which contains the desired order of the levels of the MultiIndex.
Syntax: MultiIndex.reorder_levels(order)
Parameters :
order : list containing the order of levelsReturns : A new MultiIndex
Example #1: Use MultiIndex.reorder_levels() function to reorder the levels of the MultiIndex.
# importing pandas as pdimport pandas as pd # Create the MultiIndexmidx = pd.MultiIndex.from_arrays([["Networking", "Cryptography", "Anthropology", "Science"], [88, 84, 98, 95]]) # Print the MultiIndexprint(midx) |
Выход :
Now let’s reorder the level of the MultiIndex.
# reorder the levels such that# 1st level appears before the 0thmidx.reorder_levels([1, 0]) |
Output :
As we can see in the output, the function has returned a new MultiIndex having the levels set in the passed order.
Example #2: Use MultiIndex.reorder_levels() function to reorder the levels of the MultiIndex.
# importing pandas as pdimport pandas as pd # Create the MultiIndexmidx = pd.MultiIndex.from_arrays([["Beagle", "Sephard", "Labrador", "Retriever"], [8, 4, 11, 3], ["A1", "B1", "A2", "C1"]]) # Print the MultiIndexprint(midx) |
Выход :
Now let’s reorder the levels of the MultiIndex.
# reorder the levelsmidx.reorder_levels([0, 2, 1]) |
Выход :
Как видно из выходных данных, функция вернула новый MultiIndex с уровнями, установленными в переданном порядке.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.