Signup/Sign In

Pandas DataFrame ne() Method

In this tutorial, we will learn the Python pandas DataFrame.ne() method. This method is used to get not equal to of dataframe and other, element-wise. It returns the DataFrame of bool that is the result of the comparison.

The below is the syntax of the DataFrame.ne() method.

Syntax

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

Parameters

other: It represents scalar, sequence, Series, or DataFrame. It can be any single or multiple element data structure, or list-like object.

axis: It represents index or column axis, '0' for index and '1' for the column. When the axis=0, method applied over the index axis and when the axis=1 method applied over the column axis. For the input Series , axis to match Series index on.

level: It represents an int or label. It broadcasts across a level, matching Index values on the passed MultiIndex level.

Example 1: Comparing the DataFrame in Pandas

Here, we are comparing a dataframe with a scalar value by using the DataFrame.ne() method that returns a bool-type dataframe.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
print("--------The DataFrame is---------")
print(df)
print("----After applying ne method-----")
print(df.ne(200))


--------The DataFrame is---------
A B C
0 200 60 150
1 500 250 1
----After applying ne method-----
A B C
0 False True True
1 True True True

Example 2: Comparing the DataFrame in Pandas

Here, we are comparing a dataframe with a Series using the DataFrame.ne() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
print("--------The DataFrame is---------")
print(df)
series = pd.Series([150, 200,150]) 
print("----After applying ne method-----")
print(df.ne(series,axis=0))


--------The DataFrame is---------
A B C
0 200 60 150
1 500 250 1
----After applying ne method-----
A B C
0 True True False
1 True True True
2 True True True

Example: Comparing the DataFrame with the other DataFrame

Here, we are comparing a dataframe with another DataFrame by using the DataFrame.ne() method. See the below example.

#importing pandas as pd
import pandas as pd
#creating the DataFrame
df_1=pd.DataFrame({"A":[200,500],"B":[60,250],"C":[150,1]})
df_2=pd.DataFrame({"A":[200,550],"B":[65,251],"C":[100,10]})
print("----After applying ne method-----")
print(df_1.ne(df_2))


----After applying ne method-----
A B C
0 False True True
1 True True True

Conclusion

In this tutorial, we learned the Python pandas DataFrame.ne() method. We learned the syntax, parameters and by applying this method on the DataFrame we solved examples and understood the DataFrame.ne() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.