- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
- How to apply floor division to the pandas series object by another series object?
- How to perform the Integer division operation of a pandas Series by a Python list?
- How to combine a pandas Series with Scalar using Series.combine() method?
- How to divide a pandas series with scalar using series.div() method?
- How can we apply an anonymous function to the pandas series?
- How to access a single value in pandas Series using the integer position?
- What does the apply() method do in the pandas series?
- How to access Pandas Series elements by using indexing?
- How to sort a Pandas Series?
- Python Pandas - How to select rows from a DataFrame by integer location
- How to apply the slicing indexer to the pandas DataFrame.iloc attribute?
- How to apply the slicing indexer to the pandas DataFrame.loc attribute?
- How to append elements to a Pandas series?
- How to use the apply() function for a single column in Pandas?