- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to apply floor division to the pandas series object by another series object?
The floordiv() method in the pandas series constructor is used to perform integer division of two series objects (an element-wise division operation) and the Floor Division operation is also called Integer Division, which is equivalent to // in python. The method supports the substitution of missing values in any of the inputs.
The method returns a series with resultant values, and the method has 3 parameters, which are fill_value, other, and level. The other parameter is nothing but the 2nd input object either series or a scalar.
The fill_value parameter is used to replace a specific value in the place of missing values while executing the floordiv() method; by default it will fill missing values with Nan.
Example 1
In this example, we will apply the integer division operation between two series objects by using the floordiv() method without changing any default parameter values.
# import pandas packages import pandas as pd # Creating Pandas Series objects series1 = pd.Series([57, 47, 81, 88, 43], index=['A', 'B', 'C', 'D', 'E']) print('First series object:',series1) series2 = pd.Series([1, 5, 4, 7, 9], index=['A', 'B', 'C', 'D', 'F']) print('Second series object:',series2) # apply floor division print("Floordiv of Series1 and Series2:", series1.floordiv(series2))
Output
The output is as follows −
First series object: A 57 B 47 C 81 D 88 E 43 dtype: int64 Second series object: A 1 B 5 C 4 D 7 F 9 dtype: int64 Floordiv of Series1 and Series2: A 57.0 B 9.0 C 20.0 D 12.0 E NaN F NaN dtype: float64
In the above output block, we can see the two input series objects and also the resultant one. In the resultant series, there are two Nan elements present because the value at index position “E” is not available in the second series object, as well as label “F” is not available in the called series object.
Example 2
Same as in the previous example, we have created two pandas Series with labeled indexes. After that, we applied the floordiv() method with the fill_value parameter.
# import pandas packages import pandas as pd # Creating Series objects series1 = pd.Series([10, 14, 82, 49, 82], index=['A', 'B', 'C', 'D', 'E']) print('First series object:') print(series1) series2 = pd.Series([2, 6, 4, 4, 5], index=['A', 'B', 'C', 'D', 'F']) print('Second series object:') print(series2) # Apply the floordiv method print("Floordiv of Series1 and Series2:") print(series1.floordiv(series2, fill_value=10))
Output
The output is given below −
First series object: A 10 B 14 C 82 D 49 E 82 dtype: int64 Second series object: A 2 B 6 C 4 D 4 F 5 dtype: int64 Floordiv of Series1 and Series2: A 5.0 B 2.0 C 20.0 D 12.0 E 8.0 F 2.0 dtype: float64
We can observe the output series object the Nan values are replaced with 10.