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)
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: b> 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.
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.
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