Pandas DataFrame.floordiv
Pandas DataFrame.floordiv
The DataFrame.floordiv method in pandas performs element-wise integer division (also called floor division) of a DataFrame by another DataFrame, Series, or scalar. It returns the largest integer less than or equal to the division result for each element.
Syntax
The syntax for DataFrame.floordiv is:
DataFrame.floordiv(other, axis='columns', level=None, fill_value=None)Here, DataFrame refers to the pandas DataFrame being divided.
Parameters
| Parameter | Description |
|---|---|
other | The object to divide by. Can be a scalar, Series, or DataFrame. |
axis | Specifies whether to align with rows or columns when performing the operation. Defaults to 'columns'. |
level | Broadcast across a specific level, matching Index labels. Defaults to None. |
fill_value | Specifies a value to substitute for missing values (NaN) before performing the operation. Defaults to None. |
Returns
A DataFrame containing the result of the element-wise floor division.
Examples
Basic Floor Division with a Scalar
Perform floor division of all elements in a DataFrame by a scalar value.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [10, 20, 30],
'B': [15, 25, 35],
'C': [40, 50, 60]
}
df = pd.DataFrame(data)
# Perform floor division by a scalar
print("Floor Division by 10:")
result = df.floordiv(10)
print(result)Output
Floor Division by 10:
A B C
0 1 1 4
1 2 2 5
2 3 3 6Floor Division with Another DataFrame
Perform floor division of two DataFrames element-wise.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'A': [10, 20, 30],
'B': [15, 25, 35],
'C': [40, 50, 60]
}
data2 = {
'A': [2, 4, 6],
'B': [3, 5, 7],
'C': [8, 10, 12]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform floor division between two DataFrames
print("Floor Division of Two DataFrames:")
result = df1.floordiv(df2)
print(result)Output
Floor Division of Two DataFrames:
A B C
0 5 5 5
1 5 5 5
2 5 5 5Floor Division with a Series
Perform floor division of a DataFrame by a Series. Alignment is performed based on the DataFrame's columns.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'A': [10, 20, 30],
'B': [15, 25, 35],
'C': [40, 50, 60]
}
df = pd.DataFrame(data)
# Create a Series
series = pd.Series({'A': 2, 'B': 3, 'C': 4})
# Perform floor division by a Series
print("Floor Division by a Series:")
result = df.floordiv(series)
print(result)Output
Floor Division by a Series:
A B C
0 5 5 10
1 10 8 12
2 15 11 15Handling Missing Values with fill_value
Use the fill_value parameter to fill missing values before performing the operation.
Python Program
import pandas as pd
import numpy as np
# Create a DataFrame with missing values
data = {
'A': [10, 20, np.nan],
'B': [15, np.nan, 35],
'C': [40, 50, 60]
}
df = pd.DataFrame(data)
# Perform floor division, filling missing values with 10
print("Floor Division with fill_value=10:")
result = df.floordiv(10, fill_value=10)
print(result)Output
Floor Division with fill_value=10:
A B C
0 1.0 1.0 4
1 2.0 1.0 5
2 1.0 3.0 6Summary
In this tutorial, we explored the DataFrame.floordiv method in pandas. Key takeaways include:
- Performing element-wise floor division between a DataFrame and scalar, Series, or another DataFrame.
- Handling alignment of rows or columns using the
axisparameter. - Filling missing values with
fill_valuebefore performing the operation.
The DataFrame.floordiv method is a versatile tool for performing integer division operations on pandas DataFrames.