Python | Pandas Index.union ()

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

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

Pandas Index.union() function form the union of two Index objects and sorts if possible. The function follows behaves like a standard set union operation. The function can also find the union of categorical data.

Syntax: Index.union(other)

Parameters :
other : Index or array-like

Returns : union : Index

Example #1: Use Index.union() function to find the union of two indexes.

# importing pandas as pd
import pandas as pd
  
# Creating the first index
idx1 = pd.Index([10, 20, 18, 32])
  
# Creating the second index
idx2 = pd.Index([21, 10, 30, 40, 50])
  
# Print the first Index
print(idx1)
  
# Print the second Index
print(" ", idx2)

Выход :

Let’s find the union of these two indexes

# perform set union of the two indexes
idx1.union(idx2)

Output :

The function has found the union of these two indexes.
 
Example #2: Use Index.union() function to perform set union operation on the given two indexes. Index lables are of string type.

# importing pandas as pd
import pandas as pd
  
# Creating the first index
idx1 = pd.Index(["Harry", "Mike", "Arther", "Nick"],
                                    name ="Student")
  
# Creating the second index
idx2 = pd.Index(["Alice", "Bob", "Rachel", "Tyler", "Louis"],
                                            name ="Winners")
  
# Print the first Index
print(idx1)
  
# Print the second Index
print(" ", idx2)

Выход :

Let’s find the union of these two indexes.

# find union of two indexes
idx1.union(idx2)

Выход :

Функция вернула новый индекс, который содержит результат установленного объединения idx1 и idx2.

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

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