Signup/Sign In
PUBLISHED ON: MAY 3, 2021

Python pandas Series.combine() Method

In this tutorial, we will learn the python pandas Series.combine() method. This method combines two series according to the specified method. The method takes two elements as input from the two Series and returns an element. It returns a Series which will be the result of combining a Series with the other object.

The below shows the syntax of the Series.combine() method.

Syntax

Series.combine(other, func, fill_value=None)

Parameters

other: It can be a Series or scalar. It is the value to be combined with the Series.

func: It represents the method that takes two scalars as inputs and returns an element.

fill_value: It is scalar, and it is optional.

Example: Combine Series with Scalar using the Series.combine() method

Here, in this example, we are combining the Series with the scalar element and the method is 'max'. The 'max' method takes two elements one from the Series and another one is the scalar element. It compares two elements and returns a single element which is maximum. See the below example.

The Series.combine() method returns a Series in which the elements which are less than scalar element are replaced by that scalar element.

#importing pandas as pd
import pandas as pd
series_1 = pd.Series([2,5,9,5,0,1.2,7])
print(series_1.combine(5, max))


0 5.0
1 5.0
2 9.0
3 5.0
4 5.0
5 5.0
6 7.0
dtype: float64

Example: Combine Series with Series using the Series.combine() method

Here, in this example, we are combining the Series with the Series element and the method is 'min'. The 'min' method takes two elements one from the first Series and another one from the Second Series. It compares two elements and returns a single element which is minimum. See the below example.

The Series.combine() method returns a Series by combining two series where the elements are compared with the method 'min'.

#importing pandas as pd
import pandas as pd
series_1 = pd.Series([2,5,9,5,0,1.2,7])
series_2 = pd.Series([3,1,10,1,1.1,8])
print(series_1.combine(series_2, min))


0 2.0
1 1.0
2 9.0
3 1.0
4 0.0
5 1.2
6 7.0
dtype: float64

Example: Combine Series with Series using the Series.combine() method

This example is similar to the previous one but the two series are not aligned. The Series.combine() method returns NaN because the minimum value between a scalar and the NaN is NaN. See the below example.

#importing pandas as pd
import pandas as pd
series_1 = pd.Series([2,5,5,0])
series_2 = pd.Series([3,1,10,1,1.1,8])
print(series_1.combine(series_2, min))


0 2.0
1 1.0
2 5.0
3 0.0
4 NaN
5 NaN
dtype: float64

Example: Set fill_value in the Series.combine() method

In the last example, we have seen the NaN values in the Series. We can fill those null values using the parameter fill_value in the Series.combine() method. See the below example.

First, the null values will be filled by the value '2' after that only the Series.combine() method applied 'min' method to two Series.

#importing pandas as pd
import pandas as pd
series_1 = pd.Series([2,5,5,0])
series_2 = pd.Series([3,1,10,1,1.1,8])
print(series_1.combine(series_2, min, fill_value=2))


0 2.0
1 1.0
2 5.0
3 0.0
4 1.1
5 2.0
dtype: float64

Conclusion

In this tutorial, we learned the Python pandas Series.compare() method. We learned the syntax and parameters of the Series.compare() method. We compared Series with scalar values and with another Series.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.