
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pandas Series Combine Method Explained
The pandas series combine() method is used to combine two series objects according to the specified function. The series.combine() method takes two required positional arguments. The first argument is another series object, the second argument is a function.
The method combines two elements from each series objects based on the specified function and returns that as an element of the output series object.
This method has one optional parameter which is fill_value. If the index is missing from one or another series object, then we can fill that missing index value with a specified value otherwise the value will be Nan by default.
Example 1
import pandas as pd # create pandas Series1 series1 = pd.Series([1,2,3,4,5,6]) print("First series object:",series1) # create pandas Series2 series2 = pd.Series([10,20,30,40,50,60]) print("Second series object:",series2) # combine series with max function print("combined series:",series1.combine(series2, max))
Explanation
In this example, we will combine the two series elements with the “max” function. The 'max' function takes two elements one from the series1 object and another one from the series2. It will compare both elements and return a single largest element.
Output
First series object: 0 1 1 2 2 3 3 4 4 5 5 6 dtype: int64 Second series object: 0 10 1 20 2 30 3 40 4 50 5 60 dtype: int64 combined series: 0 10 1 20 2 30 3 40 4 50 5 60 dtype: int64
The series1 and series2 objects are created by integer values, and we applied the combine() method on these two series objects. We can see the resultant series object in the above output block.
Example 2
import pandas as pd # create pandas Series1 series1 = pd.Series({'A':13,'B':48,"C":98, "D":38}) print("First series object:",series1) # create pandas Series2 series2 = pd.Series({'A':32,'B':18,"C":1, "D":85,'E':60 }) print("Second series object:",series2) # combine series with max function print("combined series:",series1.combine(series2, max))
Explanation
Initially, we have created two pandas Series objects by using python dictionaries. And then applied the combine() method with the “max” function.
Output
First series object: A 13 B 48 C 98 D 38 dtype: int64 Second series object: A 32 B 18 C 1 D 85 E 60 dtype: int64 combined series: A 32.0 B 48.0 C 98.0 D 85.0 E NaN dtype: float64
Here, the series1 and series2 are combined by using the “max” function. In this example, both series objects are having the same index labels, but the series2 is having one extra index label which is “E”. while combining these two series objects, this one extra label will not be available in another series so by default it will be filled with Nan value.