- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 integer division to the pandas series by a scalar?
The operation integer division is also called floor division, which is equivalent to // in python. And it is a binary operation nothing but an element-wise division operation that returns a new value.
In the pandas series class, there is a method called floordiv() which performs the element-wise integer division operation between a series object and a scalar. And this method can also be used to perform floor division between two series objects.
The output of this method is a new series with the result of the operation. And it has 3 parameters, which are fill_value, other, and level. The other parameter is nothing but 2nd input (other series or a scalar). The fill_value parameter is used to replace the missing values by a specified value while executing the floordiv() method, by default the parameter will fill missing values with Nan.
In this following example, we will apply the floor divide operation to the Series object by a scalar value “2”.
import pandas as pd # create pandas Series series = pd.Series([9, 25, 14, 82]) print("Series object:",series) # apply floordiv() print("Output:") print(series.floordiv(2))
The output is given below −
Series object: 0 9 1 25 2 14 3 82 dtype: int64 Output: 0 4 1 12 2 7 3 41 dtype: int64
In the above block, we can see both initial and resultant series objects. And the second one is the result of element-wise integer division operation between series and a scalar value “2”.
In this following example, we will apply the integer division operation to a series object with a scalar, the given series object contains some Nan values.
import pandas as pd import numpy as np # create pandas Series series = pd.Series([87, 5, None, 42, np.nan, 61]) print("Series object:",series) # apply floordiv() print("Output without replacing missing values:") print(series.floordiv(other=2)) # apply floordiv() method with fill_value parameter print("Output with replacing missing values by 5:") print(series.floordiv(other=2, fill_value=5))
The output is mentioned below −
Series object: 0 87.0 1 5.0 2 NaN 3 42.0 4 NaN 5 61.0 dtype: float64 Output without replacing missing values: 0 43.0 1 2.0 2 NaN 3 21.0 4 NaN 5 30.0 dtype: float64 Output with replacing missing values by 5: 0 43.0 1 2.0 2 2.0 3 21.0 4 2.0 5 30.0 dtype: float64
Initially, we have applied the floor division operation to a series object by a scalar value 2 without replacing the missing values. After that, we again applied the floordiv() method to the same series by a scalar value 2 and the missing value 5 (fill_value=5).
Both the outputs are displayed in the above output block.
Kickstart Your Career
Get certified by completing the courseGet Started