Pandas DataFrame.ne
Pandas DataFrame.ne
The DataFrame.ne method in pandas is used to compare a DataFrame element-wise with another object (such as a scalar, Series, or DataFrame) for inequality (!=). It returns a DataFrame of boolean values indicating where the inequality holds true.
Syntax
The syntax for DataFrame.ne is:
DataFrame.ne(other, axis='columns', level=None)Here, DataFrame refers to the pandas DataFrame being compared for inequality.
Parameters
| Parameter | Description |
|---|---|
other | Object to compare with (scalar, Series, or DataFrame). |
axis | Specifies whether to compare rows or columns. Default is 'columns'. |
level | Broadcasts along a particular level if the DataFrame has a MultiIndex. Default is None. |
Returns
A DataFrame of boolean values indicating where the inequality holds true.
Examples
Comparing 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 DataFrame elements with a scalar value
print("Inequality comparison with scalar 30:")
result = df.ne(30)
print(result)Output
Inequality comparison with scalar 30:
Name Age Salary
0 True True True
1 True False True
2 True True TrueComparing with Another DataFrame
Compare a DataFrame with another DataFrame 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', 'Raj', 'Priya'],
'Age': [25, 32, 35],
'Salary': [70000, 85000, 90000]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Compare the two DataFrames
print("Element-wise inequality between two DataFrames:")
result = df1.ne(df2)
print(result)Output
Element-wise inequality between two DataFrames:
Name Age Salary
0 False False False
1 True True True
2 False False FalseComparing with a Series
Compare a DataFrame with a Series element-wise 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)
# Create a Series for comparison
series = pd.Series([25, 80000, 'Priya'], index=['Age', 'Salary', 'Name'])
# Compare with the Series
print("Element-wise inequality with Series:")
result = df.ne(series, axis='columns')
print(result)Output
Element-wise inequality with Series:
Name Age Salary
0 True False True
1 True True False
2 False True TrueHandling MultiIndex with level Parameter
Use the level parameter to compare along a particular level in a MultiIndex DataFrame.
Python Program
import pandas as pd
# Create a MultiIndex DataFrame
data = {
'Value': [100, 200, 300, 400]
}
index = pd.MultiIndex.from_tuples(
[('A', 'x'), ('A', 'y'), ('B', 'x'), ('B', 'y')],
names=['Group', 'Subgroup']
)
df = pd.DataFrame(data, index=index)
# Compare with a scalar using level
print("Inequality comparison along MultiIndex level:")
result = df.ne(200, level='Subgroup')
print(result)Output
Inequality comparison along MultiIndex level:
Value
Group Subgroup
A x True
y False
B x True
y FalseSummary
In this tutorial, we explored the DataFrame.ne method in pandas. Key takeaways include:
- Using
neto perform element-wise inequality comparison with scalars, Series, or DataFrames. - Handling MultiIndex DataFrames using the
levelparameter. - Specifying the
axisto control the direction of the comparison.
The DataFrame.ne method is a powerful tool for inequality comparison in pandas, enabling flexible element-wise operations.