• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:8 mins read
You are currently viewing Pandas.Series.combine()

Pandas.Series.combine() is used to combine two series into one Series. It returns a Series having the same shape as the input series. To combine, we should take the input series with the same shapes otherwise, it will throw an error. In this article, I will explain pandas Series.combine() function and using its syntax and parameters how we can combine the given two Pandas Series into one Series with examples.

Advertisements

1. Quick Examples of Series.combine()

If you are in hurry below are some quick examples of Series.combine()


# Below are a quick examples.

# Example 1: Combine two Series using combine()
ser2 = ser.combine(ser1, (lambda x1, x2: x1 if x1 % 5 == 0 else x2))

# Example 2: Ignore NaN value using fill_value
ser2 = ser.combine(ser1, (lambda x1, x2: x1 if x1 % 5 == 0 else x2), fill_value = 5)

2. Pandas.Series.Combine() Syntax

Below is the syntax of the series combine() method of the DataFrame in Pandas.


# Series.combine() method Syntax
Series.combine(other, func, fill_value=None)

2.1 Parameters

  • other – The values are to be combined with the Series or scalar.
  • func – The function takes two scalars as inputs and returns an element.
  • fill_value – The value to assume an index is missing from one or more series. By default, it specifies using the appropriate NaN value for the underlying dtype of the Series.

2.2 Return Values

It returns a combined series with the same shape as the taken series.

3. Usage of Pandas Series.combine()

Pandas Series.combine() function is used to combine the two Pandas Series of same shape and return one Series after some mathematical calculation.


import pandas as pd
import numpy as np
# Create two Pandas series
ser = pd.Series([10, 12, 20, 25, 21])
print(ser)

ser1 = pd.Series([11, 13, 15, 17, 35])
print(ser1)
Pandas Series combine
Pandas Series

Let’s apply the combine() function to the above two Series along with the lambda function, to compare the two Series and return the combined Pandas Series of the same shape as the input Series.


# Combine two Series using combine()
ser2 = ser.combine(ser1, (lambda x1, x2: x1 if x1 % 5 == 0 else x2))
print(ser2) 
Pandas Series combine
Combined Pandas Series

4. Replace NaN Values by fill_value Param

Let’s create Pandas Series having NaN values using Numpy. Set fill_value param with'5' and pass it into combine() function along with lambda() function, it will replace NaN values with specified fill_value. For example:


# Create two Series with NaN values
ser = pd.Series([10, np.nan, 20, 25, np.nan])
print(ser)

ser1 = pd.Series([np.nan, 13, 15, np.nan, 35])
print(ser1)

# Ignore NaN value using fill_value
ser2 = ser.combine(ser1, (lambda x1, x2: x1 if x1 % 5 == 0 else x2), fill_value = 5)
print(ser2)

Yields below output.


# Output:
First Series:
 0    10.0
 1     NaN
 2    20.0
 3    25.0
 4     NaN
dtype: float64

Second Series:
0     NaN
1    13.0
2    15.0
3     NaN
4    35.0
dtype: float64

Combined Series:
0    10.0
1    13.0
2    20.0
3    25.0
4    35.0
dtype: float64

5. Conclusion

In this article, I have explained pandas Series.combine() function and using its syntax and parameters how to combine the given two Pandas Series into one Series with examples.

References