Pandas DataFrame.eq
Pandas DataFrame.eq
The DataFrame.eq
method in pandas is used to perform element-wise equality comparison between a DataFrame and another DataFrame, Series, or scalar value. It returns a DataFrame of the same shape with True
where elements are equal and False
otherwise.
Syntax
The syntax for DataFrame.eq
is:
DataFrame.eq(other, axis='columns', level=None)
Here, DataFrame
refers to the pandas DataFrame being compared.
Parameters
Parameter | Description |
---|---|
other | Value to compare against. Can be a scalar, Series, or DataFrame. |
axis | Specifies whether to compare by rows or columns. Defaults to 'columns' . Use 0 or 'index' for row-wise comparison and 1 or 'columns' for column-wise comparison. |
level | Broadcast across a specified level, matching labels on a MultiIndex. |
Returns
A DataFrame of the same shape as the original, with boolean values indicating element-wise equality.
Examples
Comparing with a Scalar
Compare all elements in a DataFrame to 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 with a scalar value
print("Comparing all elements with 30:")
result = df.eq(30)
print(result)
Output
Comparing all elements with 30:
Name Age Salary
0 False False False
1 False True False
2 False False False
Row-Wise Comparison
Compare elements of a DataFrame with a Series 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
row_comparison = pd.Series({'Name': 'Arjun', 'Age': 25, 'Salary': 70000})
print("Row-wise comparison:")
result = df.eq(row_comparison, axis=1)
print(result)
Output
Row-wise comparison:
Name Age Salary
0 True True True
1 False False False
2 False False False
Column-Wise Comparison
Compare elements of a DataFrame with a Series column-wise using axis=1
.
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 column-wise with a Series
column_comparison = pd.Series(['Arjun', 30, 80000], index=['Name', 'Age', 'Salary'])
print("Column-wise comparison:")
result = df.eq(column_comparison, axis=0)
print(result)
Output
Column-wise comparison:
Name Age Salary
0 True False False
1 False True True
2 False False False
Comparing with Another DataFrame
Compare two DataFrames element-wise.
Python Program
import pandas as pd
# Create two DataFrames
data1 = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'Salary': [70000, 80000, 90000]
}
data2 = {
'Name': ['Arjun', 'Ram', 'Suresh'],
'Age': [25, 29, 35],
'Salary': [70000, 75000, 90000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Compare two DataFrames
print("Element-wise comparison between two DataFrames:")
result = df1.eq(df2)
print(result)
Output
Element-wise comparison between two DataFrames:
Name Age Salary
0 True True True
1 True False False
2 False True True
Summary
In this tutorial, we explored the DataFrame.eq
method in pandas. Key takeaways include:
- Using
eq
for element-wise equality comparisons with scalars, Series, or other DataFrames. - Performing row-wise or column-wise comparisons with the
axis
parameter. - Handling element-wise comparisons with flexible inputs.
The DataFrame.eq
method is a powerful tool for equality checks and filtering data in pandas.