Python | Pandas dataframe.ne()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas dataframe.ne() function checks for inequality of a dataframe element with a constant, series or other dataframe element-wise. If two values in comparison are not equal to each other, it returns a true else if they are equal it returns false.

Syntax: DataFrame.ne(other, axis=’columns’, level=None)



Parameters :
other : Series, DataFrame, or constant
axis : For Series input, axis to match Series index on
level :Broadcast across a level, matching Index values on the passed MultiIndex level

Returns : result : DataFrame

Example #1: Use ne() function to check for inequality between series and a dataframe.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the first dataframe 
df1=pd.DataFrame({"A":[14,4,5,4,1],
                  "B":[5,2,54,3,2],
                  "C":[20,20,7,3,8],
                  "D":[14,3,6,2,6]})
  
# Print the dataframe
df1

chevron_right


Let’s create the series

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# create series
sr = pd.Series([3, 2, 4, 5, 6])
  
# Print series
sr

chevron_right


Lets use the dataframe.ne() function to evaluate for inequality

filter_none

edit
close

play_arrow

link
brightness_4
code

# evaluate inequality over the index axis
df.ne(sr, axis = 0)

chevron_right


Output :

All true value cells indicate that values in comparison are not equal to each other whereas, all the false values cells indicate that values in comparison are equal to each other.
 
Example #2: Use ne() function to check for inequality of two datframes. One dataframe contains NA values.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the first dataframe 
df1=pd.DataFrame({"A":[14,4,5,4,1],
                  "B":[5,2,54,3,2],
                  "C":[20,20,7,3,8],
                  "D":[14,3,6,2,6]})
  
# Creating the second dataframe with <code>Na</code> value
df2=pd.DataFrame({"A":[12,4,5,None,1],
                  "B":[7,2,54,3,None],
                  "C":[20,16,11,3,8],
                  "D":[14,3,None,2,6]})
  
# Print the second dataframe
df2

chevron_right


Let’s use the dataframe.ne() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

# passing df2 to check for inequality with the df1 dataframe.
d1f.ne(df2)

chevron_right


Output :

All true value cells indicate that values in comparison are not equal to each other whereas, all the false values cells indicate that values in comparison are equal to each other.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.