Pandas DataFrame.le
Pandas DataFrame.le
The DataFrame.le method in pandas is used to perform an element-wise less-than-or-equal-to comparison between a DataFrame and another DataFrame, Series, or scalar value. The method returns a DataFrame of boolean values, where each element indicates if the corresponding element in the original DataFrame is less than or equal to the specified value.
Syntax
The syntax for DataFrame.le is:
DataFrame.le(other, axis='columns', level=None)Here, DataFrame refers to the pandas DataFrame on which the comparison is being applied.
Parameters
| Parameter | Description |
|---|---|
other | Value to compare with. Can be a scalar, Series, or DataFrame. |
axis | Determines the axis along which the comparison is performed. Defaults to 'columns'. Use 0 or 'index' for row-wise comparison, and 1 or 'columns' for column-wise comparison. |
level | Used when comparing with a multi-level object (like a DataFrame with a MultiIndex). Defaults to None. |
Returns
A DataFrame of boolean values indicating the result of the less-than-or-equal-to comparison.
Examples
Comparing with a Scalar
Perform a less-than-or-equal-to comparison between a DataFrame and a scalar value.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Compare all elements to a scalar value
print("Comparing with scalar value 30:")
result = df[['Age', 'Salary']].le(30)
print(result)Output
Comparing with scalar value 30:
Age Salary
0 True False
1 True False
2 False FalseComparing with Another DataFrame
Perform a less-than-or-equal-to comparison between two DataFrames.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
data2 = {
'Age': [30, 25, 40],
'Salary': [75000, 75000, 95000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Compare the two DataFrames
print("Element-wise comparison between two DataFrames:")
result = df1.le(df2)
print(result)Output
Element-wise comparison between two DataFrames:
Age Salary
0 True True
1 False True
2 True TrueComparing Along a Specific Axis
Perform a less-than-or-equal-to comparison along a specific axis.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)
# Compare row-wise with a Series
series = pd.Series([30, 75000], index=['Age', 'Salary'])
print("Row-wise comparison with a Series:")
result = df[['Age', 'Salary']].le(series, axis='columns')
print(result)Output
Row-wise comparison with a Series:
Age Salary
0 True True
1 True False
2 False FalseUsing MultiIndex and Level
Use the level parameter to compare specific levels of a MultiIndex DataFrame.
Python Program
import pandas as pd
# Create a MultiIndex DataFrame
data = {
'Value': [10, 20, 30, 40]
}
index = pd.MultiIndex.from_tuples([
('A', 1), ('A', 2), ('B', 1), ('B', 2)
], names=['Group', 'Number'])
df = pd.DataFrame(data, index=index)
# Compare values at level 'Number'
print("Comparison at level 'Number':")
result = df.le(20, level='Number')
print(result)Output
Comparison at level 'Number':
Value
Group Number
A 1 True
2 False
B 1 True
2 FalseSummary
In this tutorial, we explored the DataFrame.le method in pandas. Key points include:
- Comparing a DataFrame with a scalar, Series, or another DataFrame.
- Using the
axisparameter for row-wise or column-wise comparisons. - Handling MultiIndex DataFrames with the
levelparameter.
The DataFrame.le method is a powerful tool for element-wise comparisons in pandas, useful for filtering and conditional operations.