Python | Панды Series.combine ()
Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.
Pandas Series.combine() function combine the Series with a Series or scalar according to func. It combine the Series and other using func to perform element-wise selection for combined Series. fill_value is assumed when value is missing at some index from one of the two objects being combined.
Syntax: Series.combine(other, func, fill_value=None)
Parameter :
other : Series or scalar
func : Function that takes two scalars as inputs and returns an element.
fill_value : The value to assume when an index is missing from one Series or the other.Returns : Series
Example #1: Use Series.combine() function to find the maximum value for each index labels in the two series object.
# importing pandas as pdimport pandas as pd # Creating the first Seriessr1 = pd.Series([80, 25, 3, 25, 24, 6]) # Creating the second Seriessr2 = pd.Series([34, 5, 13, 32, 4, 15]) # Create the Indexindex_ = ["Coca Cola", "Sprite", "Coke", "Fanta", "Dew", "ThumbsUp"] # set the first indexsr1.index = index_ # set the second indexsr2.index = index_ # Print the first seriesprint(sr1) # Print the second seriesprint(sr2) |
Output :

Now we will use Series.combine() function to find the maximum value for each index labels in the two given series object.
# find the maximum element-wise# among sr1 and sr2result = sr1.combine(other = sr2, func = max) # Print the resultprint(result) |
Output :
As we can see in the output, the Series.combine() function has successfully returned the maximum value for each index labels among the two series objects.
Example #2 : Use Series.combine() function to find the minimum value for each index labels in the two series object.
# importing pandas as pdimport pandas as pd # Creating the first Seriessr1 = pd.Series([51, 10, 24, 18, None, 84, 12, 10, 5, 24, 2]) # Creating the second Seriessr2 = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None]) # Create the Indexindex_ = pd.date_range("2010-10-09", periods = 11, freq ="M") # set the first indexsr1.index = index_ # set the second indexsr2.index = index_ # Print the first seriesprint(sr1) # Print the second seriesprint(sr2) |
Output :

Now we will use Series.combine() function to find the minimum value for each index labels in the two given series object.
# find the minimum element-wise# among sr1 and sr2result = sr1.combine(other = sr2, func = min) # Print the resultprint(result) |
Выход :
As we can see in the output, the Series.combine() function has successfully returned the minimum value for each index labels among the two series objects.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.