Python | Pandas Series.combine()
Last Updated :
17 Feb, 2019
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
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.
import pandas as pd
sr1 = pd.Series([ 80 , 25 , 3 , 25 , 24 , 6 ])
sr2 = pd.Series([ 34 , 5 , 13 , 32 , 4 , 15 ])
index_ = [ 'Coca Cola' , 'Sprite' , 'Coke' , 'Fanta' , 'Dew' , 'ThumbsUp' ]
sr1.index = index_
sr2.index = index_
print (sr1)
print (sr2)
|
Output :
Now we will use Series.combine()
function to find the maximum value for each index labels in the two given series object.
result = sr1.combine(other = sr2, func = max )
print (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.
import pandas as pd
sr1 = pd.Series([ 51 , 10 , 24 , 18 , None , 84 , 12 , 10 , 5 , 24 , 2 ])
sr2 = pd.Series([ 11 , 21 , 8 , 18 , 65 , 18 , 32 , 10 , 5 , 32 , None ])
index_ = pd.date_range( '2010-10-09' , periods = 11 , freq = 'M' )
sr1.index = index_
sr2.index = index_
print (sr1)
print (sr2)
|
Output :
Now we will use Series.combine()
function to find the minimum value for each index labels in the two given series object.
result = sr1.combine(other = sr2, func = min )
print (result)
|
Output :
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.
Share your thoughts in the comments
Please Login to comment...