Built with Tutify.io

    AIStudyLabs

    Pandas.Series.combine() Function

    Pandas.Series.combine() Function

    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 –

    <a id="h-quick-examples-of-series-combine"></a>

    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)
    

    <a id="h-pandas-series-combine-syntax"></a>

    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)
    

    <a id="h-parameters"></a>

    Parameters

    <a id="h-return-values"></a>

    Return Values

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

    <a id="h-usage-of-pandas-series-combine"></a>

    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)
    

    <a id="h-replace-nan-values-by-fill-value-param"></a>

    Replace NaN Values by fill_value Param

    Let's create Pandas Series having NaN values usingNumpy. 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
    

    <a id="h-faq-on-pandas-series-combine-function"></a>

    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.

    <a id="h-conclusion"></a>

    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.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References