
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 divmod Method Explained
The divmod() method in the pandas series constructor is used to perform integer division and modulo of two series objects. We can calculate the divmod() of one series with a scalar value. And we can perform element-wise divmod() operation.
The method returns a python tuple with two series objects, the first series of the tuple is representing the integer division results, and the second series object of the tuple representing the modulo division results.
The method performs an element-wise division operation of two series objects. There is a parameter called fill_value, which is used to fill the specified values in the place of missing values while executing the divmod() method, by default it will fill missing values with Nan.
Example 1
# import pandas packages import pandas as pd # Creating Series objects series1 = pd.Series([96, 75, 23, 17, 30], index=['A', 'B', 'C', 'D', 'E']) print('First series object:',series1) series2 = pd.Series([8, 6, 4, 5, 3], index=['A', 'B', 'C', 'D', 'F']) print('second series object:',series2) print("Divmod of Series1 and Series2:", series1.divmod(series2))
Explanation
In this example, we will divide two series objects by using the divmod() method without changing any default parameter values.
Output
First series object: A 96 B 75 C 23 D 17 E 30 dtype: int64 second series object: A 8 B 6 C 4 D 5 F 3 dtype: int64 Divmod of Series1 and Series2: (A 12.0 B 12.0 C 5.0 D 3.0 E NaN F NaN dtype: float64, A 0.0 B 3.0 C 3.0 D 2.0 E NaN F NaN dtype: float64)
In the above output block, we can see the tuple of two series objects. The First object represents the integer division values and the second one represents the modulo values of the divmod() method on two series objects.
Example 2
# import pandas packages import pandas as pd # Creating Series objects series1 = pd.Series([36, 85, 4, 13, 34], index=['A', 'B', 'C', 'D', 'E']) print('First series object:',series1) series2 = pd.Series([7, 8, 1, 6, 8], index=['A', 'B', 'C', 'D', 'F']) print('second series object:',series2) print("Divmod of Series1 and Series2:", series1.divmod(series2, fill_value=10))
Explanation
Initially, we have created two pandas Series with labeled indexes. After that, we applied the divmod() method with the fill_value parameter.
Output
First series object: A 36 B 85 C 4 D 13 E 34 dtype: int64 second series object: A 7 B 8 C 1 D 6 F 8 dtype: int64 Divmod of Series1 and Series2: (A 5.0 B 10.0 C 4.0 D 2.0 E 3.0 F 1.0 dtype: float64, A 1.0 B 5.0 C 0.0 D 1.0 E 4.0 F 2.0 dtype: float64)
In the above output block, we can see the tuple of two series objects. While executing divmod() method the Nan values are replaced with 10, which is defined by the fill_value parameter.