Pandas DataFrame.gt


Pandas DataFrame.gt

The DataFrame.gt method in pandas is used to compare whether elements of a DataFrame are greater than a specified value or corresponding elements in another DataFrame. The result is a DataFrame of boolean values indicating the comparison results.


Syntax

The syntax for DataFrame.gt is:

DataFrame.gt(other, axis='columns', level=None)

Here, DataFrame refers to the pandas DataFrame being compared.


Parameters

ParameterDescription
otherA scalar, sequence, Series, or DataFrame to compare against.
axisThe axis to align the comparison. 'columns' (default) compares element-wise by column, and 'index' compares by row.
levelUsed for broadcasting in a MultiIndex DataFrame. Specifies the level to align along.

Returns

A DataFrame of boolean values indicating where the comparison is True.


Examples

Comparing with a Scalar Value

Compare each element in the 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 greater than 30
print("Elements greater than 30:")
result = df.gt(30)
print(result)

Output

Elements greater than 30:
    Name    Age  Salary
0  False  False    True
1  False  False    True
2  False   True    True

Comparing with Another DataFrame

Compare the elements of a DataFrame with those of another DataFrame of the same shape.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
data2 = {
    'Age': [20, 32, 33],
    'Salary': [75000, 75000, 95000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Compare elements of the two DataFrames
print("Comparison of df1 > df2:")
result = df1.gt(df2)
print(result)

Output

Comparison of df1 > df2:
     Age  Salary
0   True   False
1  False    True
2   True   False

Comparing Along Rows (axis='index')

Perform element-wise comparison along rows by specifying axis='index'.

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 each row with a Series
comparison_row = pd.Series([30, 75000], index=['Age', 'Salary'])

print("Comparison along rows:")
result = df[['Age', 'Salary']].gt(comparison_row, axis='index')
print(result)

Output

Comparison along rows:
     Age  Salary
0  False   False
1   True    True
2   True    True

Handling MultiIndex with level

Use the level parameter to align comparisons in MultiIndex DataFrames.

Python Program

import pandas as pd

# Create a MultiIndex DataFrame
data = {
    'Values': [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 using level
comparison = pd.Series([15, 35], index=['A', 'B'])

print("Comparison with level alignment:")
result = df.gt(comparison, level='Group')
print(result)

Output

Comparison with level alignment:
               Values
Group Number         
A     1         False
      2          True
B     1         False
      2          True

Summary

In this tutorial, we explored the DataFrame.gt method in pandas. Key points include:

  • Using gt to compare DataFrame elements with scalar values, Series, or other DataFrames.
  • Specifying axis for alignment along rows or columns.
  • Handling MultiIndex DataFrames with the level parameter.

The DataFrame.gt method is a powerful tool for element-wise comparison in pandas DataFrames.


Python Libraries