Python | Панды MultiIndex.from_tuples ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas MultiIndex.from_tuples() function is used to convert list of tuples to MultiIndex. It is one of the several ways in which we construct a MultiIndex.
Syntax: MultiIndex.from_tuples(tuples, sortorder=None, names=None)
Parameters :
tuples : Each tuple is the index of one row/column.
sortorder : Level of sortedness (must be lexicographically sorted by that level)Returns: index : MultiIndex
Example #1: Use MultiIndex.from_tuples() function to construct a MultiIndex using python tuples.
# importing pandas as pdimport pandas as pd # Creating the Tupletuples =[(30, "Larry"), (20, "Mike"), (18, "David"), (25, "Tim")] # Print the Tupleprint(tuples) |
Выход :
Now let’s create the MultiIndex using the Tuples.
# Creating the MultiIndexmidx = pd.MultiIndex.from_tuples(tuples, names =("Age", "Name")) # Print the MultiIndexprint(midx) |
Output :
As we can see in the output, the function has created a MultiIndex object using the Tuples.
Example #2: Use MultiIndex.from_tuples() function to construct a MultiIndex using python tuples.
# importing pandas as pdimport pandas as pd # Creating the Tupletuples =[("Physics", 85), ("Chemistry", 88), ("Maths", 95), ("Computers", 99)] # Print the Tupleprint(tuples) |
Выход :
Now let’s create the MultiIndex using the Tuples.
# Creating the MultiIndexmidx = pd.MultiIndex.from_tuples(tuples, names =("Subject", "Marks")) # Print the MultiIndexprint(midx) |
Выход :
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.