w3resource

Pandas Series: combine() function

Combine the Series with a Series in Pandas

The combine() function is used to combine the Series with a Series or scalar according to func.

Combine the Series and other using func to perform elementwise selection for combined Series. fill_value is assumed when value is missing at some index from one of the two objects being combined.

Syntax:

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

Parameters:

Name Description Type/Default Value Required / Optional
other The value(s) to be combined with the Series.
Series or scalar value Required
func  Function that takes two scalars as inputs and returns an element. function Required
fill_value The value to assume when an index is missing from one Series or the other. The default specifies to use the appropriate NaN value for the underlying dtype of the Series. scalar optional

Returns: Series
The result of combining the Series with the other object.

Example - Consider 2 Datasets s1 and s2 containing highest clocked speeds of different birds:

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series({'eagle': 320.0, 'parrot': 120.0})
s1

Output:

eagle     320.0
parrot    120.0
dtype: float64
Pandas Series combine() function

Python-Pandas Code:

import numpy as np
import pandas as pd
s2 = pd.Series({'eagle': 335.0, 'parrot': 180.0, 'sparrow': 100.0})
s2

Output:

eagle      335.0
parrot     180.0
sparrow    100.0
dtype: float64
Pandas Series combine() function
Now, to combine the two datasets and view the highest speeds of the birds across the two datasets

Output:

File "<ipython-input-4-a144bc87c1a0>", line 1
    Now, to combine the two datasets and view the highest speeds of the birds across the two datasets
                  ^
SyntaxError: invalid syntax

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series({'eagle': 320.0, 'parrot': 120.0})
s2 = pd.Series({'eagle': 335.0, 'parrot': 180.0, 'sparrow': 100.0})
s1.combine(s2, max)

In the previous example, the resulting value for sparrow is missing, because the maximum of a NaN and a float is a NaN.

So, in the example, we set fill_value=0, so the maximum value returned will be the value from some dataset.

Python-Pandas Code:

import numpy as np
import pandas as pd
s1 = pd.Series({'eagle': 320.0, 'parrot': 120.0})
s2 = pd.Series({'eagle': 335.0, 'parrot': 180.0, 'sparrow': 100.0})
s1.combine(s2, max, fill_value=0)

Previous: Exponential power of Pandas series
Next: Combine Series values in Pandas



Follow us on Facebook and Twitter for latest update.