Pandas DataFrame.ge
Pandas DataFrame.ge
The DataFrame.ge
method in pandas is used to compare elements of a DataFrame with another DataFrame, Series, or scalar value. The method performs element-wise greater than or equal to (>=
) comparisons, returning a DataFrame of boolean values.
Syntax
The syntax for DataFrame.ge
is:
DataFrame.ge(other, axis='columns', level=None)
Here, DataFrame
refers to the pandas DataFrame being compared.
Parameters
Parameter | Description |
---|---|
other | A scalar, Series, or DataFrame to compare with. |
axis | Determines the axis along which the comparison is made. Default is 'columns' . Use 0 or 'index' for row-wise comparison. |
level | If the axis is a MultiIndex, specifies the level to align comparisons. |
Returns
A DataFrame of boolean values indicating the result of the greater than or equal to comparison for each element.
Examples
Comparing with a Scalar
Compare all elements in 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 value
print("Comparing with scalar value 30:")
result = df[['Age', 'Salary']].ge(30)
print(result)
Output
Comparing with scalar value 30:
Age Salary
0 False True
1 True True
2 True True
Comparing with Another DataFrame
Perform element-wise comparison with another DataFrame of the same shape.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
data2 = {
'Age': [20, 30, 40],
'Salary': [75000, 75000, 95000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Perform element-wise comparison
print("Comparing with another DataFrame:")
result = df1[['Age', 'Salary']].ge(df2)
print(result)
Output
Comparing with another DataFrame:
Age Salary
0 True False
1 True True
2 False False
Comparing Row-wise Using axis=0
Compare elements row-wise using axis=0
.
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
compare_series = pd.Series([25, 75000], index=['Age', 'Salary'])
print("Comparing row-wise with a Series:")
result = df[['Age', 'Salary']].ge(compare_series, axis=1)
print(result)
Output
Comparing row-wise with a Series:
Age Salary
0 True False
1 True True
2 True True
Handling MultiIndex
Perform element-wise comparison on a DataFrame with a MultiIndex using the level
parameter.
Python Program
import pandas as pd
# Create a MultiIndex DataFrame
data = {
'Value': [5, 15, 25, 35]
}
index = pd.MultiIndex.from_tuples([
('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two')
], names=['Group', 'Number'])
df = pd.DataFrame(data, index=index)
# Compare with scalar, specifying level
print("Comparing with scalar 20 at level 'Group':")
result = df.ge(20, level='Group')
print(result)
Output
Comparing with scalar 20 at level 'Group':
Value
Group Number
A one False
two False
B one True
two True
Summary
In this tutorial, we explored the DataFrame.ge
method in pandas. Key takeaways include:
- Performing element-wise greater than or equal to comparisons with scalars, Series, or DataFrames.
- Using the
axis
parameter for row-wise or column-wise comparison. - Handling comparisons on MultiIndex DataFrames using the
level
parameter.
The DataFrame.ge
method is a flexible tool for filtering and comparing DataFrame values in pandas.