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.
Key Points –
- Combines two Series objects using a specified function to perform element-wise operations.
- The specified function is applied to each pair of corresponding elements in the two Series.
- Supports a
fill_value
parameter to substitute missing values (NaNs) during the combination process. - Returns a new Series that contains the results of the function applied to the input Series.
- Allows users to define custom binary functions for flexible and tailored element-wise operations.
- If the Series have different indices, the result will include the union of the indices.
Quick Examples of Series.combine()
If you are in hurry below are some quick examples of Series.combine()
# Quick examples of Series.combine()
# 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)
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)
Parameters
other
– The values are to be combined with theSeries
orscalar
.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.
Return Values
It returns a combined series with the same shape as the taken series.
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)
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)
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
FAQ on Pandas.Series.combine() Function
The combine()
function in pandas is used to combine two Series
objects element-wise using a specified function. This function is applied to the corresponding elements from both Series
. It allows flexible operations like merging, updating, or applying custom logic to combine the series.
If the Series
have different indices, pandas will align them by index before applying the function. Missing values will be treated as NaN
. You can control how missing values are handled using the fill_value
parameter.
The combine()
function can handle missing data. If a value is missing in one of the Series
, you can use the fill_value
parameter to replace missing values before the function is applied.
Series.combine()
is more flexible as it allows applying a custom function to combine two Series
. In contrast, Series.update()
is primarily used to update values in the first Series
with values from the second Series
, where the second Series
provides non-null values.
You can use built-in functions like max()
or min()
with combine()
. For example, you can combine two Series
using the max
function to get the maximum value for each index.
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.
Related Articles
- How to create Pandas Series in Python
- Pandas Window Functions Explained
- How to Create Scatter Plot in Pandas?
- How to Generate Time Series Plot in Pandas
- How to Plot the Boxplot from DataFrame?
- Pandas Handle Missing Data in Dataframe
- How to Change Pandas Plot Size?
- How to add title to Pandas plots?
- How to generate line plot in Pandas?
- How to add legends to plots in Pandas
- How to create Pandas histogram plot?
- How to plot Pandas Series?
- How to distribute the column values in Pandas plot?