Pandas DataFrame.lt
Pandas DataFrame.lt
The DataFrame.lt
method in pandas is used to perform element-wise comparison, checking whether each element in a DataFrame is less than the corresponding element in another DataFrame or scalar value. This method is a shorthand for the less-than (<) operator and returns a DataFrame of boolean values indicating the comparison result.
Syntax
The syntax for DataFrame.lt
is:
DataFrame.lt(other, axis='columns', level=None)
Here, DataFrame
refers to the pandas DataFrame being compared.
Parameters
Parameter | Description |
---|---|
other | A scalar, sequence, or DataFrame to compare with. |
axis | Axis along which the comparison is performed. 'columns' (default) compares element-wise along columns, and 'index' compares element-wise along rows. |
level | If the axis is a MultiIndex (hierarchical), comparison can be performed on a particular level. Defaults to None . |
Returns
A DataFrame of boolean values indicating where the condition (<) is satisfied.
Examples
Element-Wise Comparison with a Scalar
Compare all elements of a DataFrame with 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 elements with a scalar
print("Is each element in 'Age' and 'Salary' less than 30,000?")
result = df[['Age', 'Salary']].lt(30000)
print(result)
Output
Is each element in 'Age' and 'Salary' less than 30,000?
Age Salary
0 True False
1 False False
2 False False
Element-Wise Comparison with Another DataFrame
Compare two DataFrames element-wise to check for less-than conditions.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
data2 = {
'Age': [30, 25, 40],
'Salary': [75000, 75000, 85000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Compare the two DataFrames
print("Element-wise comparison of 'Age' and 'Salary':")
result = df1[['Age', 'Salary']].lt(df2)
print(result)
Output
Element-wise comparison of 'Age' and 'Salary':
Age Salary
0 True True
1 False False
2 False False
Comparison Along Rows
Use axis='index'
to perform element-wise comparison along rows.
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 along rows
print("Comparison of 'Age' with 'Salary' along rows:")
result = df[['Age', 'Salary']].lt(df['Salary'], axis='index')
print(result)
Output
Comparison of 'Age' with 'Salary' along rows:
Age Salary
0 True False
1 True False
2 True False
Handling MultiIndex Levels
Use the level
parameter to compare elements at a specific level in a MultiIndex DataFrame.
Python Program
import pandas as pd
# Create a MultiIndex DataFrame
data = {
'Age': [25, 30, 35, 40],
'Salary': [70000, 80000, 90000, 100000]
}
index = pd.MultiIndex.from_tuples([
('Group1', 'Arjun'),
('Group1', 'Ram'),
('Group2', 'Priya'),
('Group2', 'Karan')
], names=['Group', 'Name'])
df = pd.DataFrame(data, index=index)
# Compare elements at the 'Group' level
print("Comparison of 'Age' < 35 at 'Group' level:")
result = df['Age'].lt(35, level='Group')
print(result)
Output
Comparison of 'Age' < 35 at 'Group' level:
Group Name
Group1 Arjun True
Ram True
Group2 Priya False
Karan False
Name: Age, dtype: bool
Summary
In this tutorial, we explored the DataFrame.lt
method in pandas. Key takeaways include:
- Using
lt
to perform element-wise less-than comparisons. - Comparing with scalars, DataFrames, or along specific axes.
- Utilizing MultiIndex levels for advanced comparisons.
The DataFrame.lt
method is a versatile tool for applying conditional logic to pandas DataFrames.