  # Python | Pandas Series.combine ()

NumPy | Python Methods and Functions

Pandas `Series.combine()` — it is a series mathematical operation method. This is used to combine two series into one. The shape of the output series is the same as that of the caller series. The elements are defined by the function passed as a parameter to the ` combine () ` method. The shape of both series must be the same, otherwise it will lead to an error.

Syntax: Series.combine (other, func, fill_value = nan)

Parameters:
other: other series or list type to be combined with caller series
func: Function passed as parameter which will decide from which series the element should be put at that index
fill_value: integer value of level in case of multi index

Return: Combined series with same shape as caller series

Example # 1:

In this example, two lists are created and converted into a series pandas using the .Series () method. The function is created using a lambda that checks which values ​​are lower in both series and returns which is lower.

 ` # pandas module import ` ` import ` ` pandas as pd `   ` # create the first episode ` ` first ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 3 ` `, ` ` 7 ` `, ` ` 11 ` `, ` ` 0 , 4 ] ``   # create a second episode second = [ 5 , 3 , 2 , 1 , 3 , 9 , 21 , 3 , 1 ]   # create series first =  pd.Series (first)   # serial creation second = pd.Series (second)   # calling the .combine () method result = first.combine (second, ( lambda x1, x2: x1 if x1 & lt; x2 else x2))   # display result `

Output:
As shown in the output image, the returned row has lower values ​​from both rows. Example # 2:

In this example, NULL values ​​are also passed from the ` Numpy.nan ` method of the ` Numpy.nan ` method. Since the series contains zero values, 5 is passed to the fill_value parameter to replace the zero values ​​with 5. A lambda function is passed that compares the values ​​in both series and returns a larger value.

` `

` # pandas module import import pandas as pd   # numpy module import import numpy as np   # create the first episode first = [ 1 , 2 , np.nan, 5 , 6 , 3 , np.nan, 7 , 11 , 0 , 4 , 8 ]   # create the second episode second = [ 5 , 3 , 2 , np.nan, 1 , 3 , 9 , 21 , 3 , np.nan, 1 , np.nan]    # create series first = pd.Series (first)   # create the series second = pd.Series (second)   # method call .combine () result = first.combine (second , func = ( lambda x1, x2: x1 if  x1 & gt; x2 else x2), fill_value = 5 )   # display result `

` `

Output:
As shown in the output, the NaN values ​​in the series were replaced by 5 before the series was merged.

` 0 5.0 1 3.0 2 2.0 3 5.0 4 6.0 5 3.0 6 9.0 7 21.0 8 11.0 9 5.0 10 4.0 11 8.0 dtype: float64 `