Python | Pandas MultiIndex.names

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas MultiIndex.names attribute returns the names of levels in the MultiIndex.

Syntax: MultiIndex.names

Example #1: Use MultiIndex.names attribute to find the names of the levels in the MultiIndex.

# importing pandas as pd
import pandas as pd
  
# Creating the array
array = [[1, 2, 3], ["Sharon", "Nick", "Bailey"]]
  
# Print the array
print(array)

Выход :

Now let’s create the MultiIndex using this array

# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, names =("Number", "Names"))
  
# Print the MultiIndex
print(midx)

Выход :

Now we will find the names of all the levels in the MultiIndex.

# Print the names of the level in MultiIndex
midx.names

Output :

As we can see in the output, midx has two levels and the name of the levels are ‘Number’ and ‘Names’.
 
Example #2: Use MultiIndex.names attribute to find the names of the levels in the given MultiIndex.

# importing pandas as pd
import pandas as pd
  
# Creating the array
array =[[1, 2, 3], ["Sharon", "Nick", "Bailey"], 
           ["Doctor", "Scientist", "Physicist"]]
  
# Print the array
print(array)

Выход :

Now let’s create the MultiIndex using this array

# Creating the MultiIndex
midx = pd.MultiIndex.from_arrays(array, 
   names =("Ranking", "Names", "Profession"))
  
# Print the MultiIndex
print(midx)

Выход :

Now we will find the names of all the levels in the MultiIndex.

# Print the names of the level in MultiIndex
midx.names

Выход :

Как видно из выходных данных, midx имеет три уровня, и названия уровней - «Число», «Имена» и «Профессия».

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.