Python | Панды MultiIndex.set_labels ()
Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.
Pandas MultiIndex.set_labels() function set new labels on MultiIndex. Defaults to returning new index.
Syntax: MultiIndex.set_labels(labels, level=None, inplace=False, verify_integrity=True)
Parameters :
labels : new labels to apply
level : level(s) to set (None for all levels)
inplace : if True, mutates in place
verify_integrity : if True, checks that levels and labels are compatibleReturns: new index (of same type and class…etc)
Example #1: Use MultiIndex.set_labels() function to reset the lables of the MultiIndex.
# importing pandas as pdimport pandas as pd # Create the MultiIndexmidx = pd.MultiIndex.from_tuples([(10, "Ten"), (10, "Twenty"), (20, "Ten"), (20, "Twenty")], names =["Num", "Char"]) # Print the MultiIndexprint(midx) |
Выход :
Now let’s reset the labels of the MultiIndex.
# resetting the labels the MultiIndexmidx.set_labels([[1, 1, 0, 0], [0, 1, 1, 0]]) |
Output :
As we can see in the output, the labels of the MultiIndex has been reset.
Example #2: Use MultiIndex.set_labels() function to reset any specific label only in the MultiIndex.
# importing pandas as pdimport pandas as pd # Create the MultiIndexmidx = pd.MultiIndex.from_tuples([(10, "Ten"), (10, "Twenty"), (20, "Ten"), (20, "Twenty")], names =["Num", "Char"]) # Print the MultiIndexprint(midx) |
Выход :
Now let’s reset the ‘Char’ label of the MultiIndex.
# resetting the labels the MultiIndexmidx.set_labels([0, 1, 1, 0], level ="Char") |
Выход :
Как видно из вывода, метка Char в MultiIndex была сброшена на желаемое значение.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.