Python Pandas DataFrame ne() Function

Pandas DataFrame ne() Function:

The ne() function of the Pandas compares dataframes and other element-by-element for not equal to and returns the outcome of the comparison. It’s the same as dataframe!= other, but with the ability to select the comparison axis (rows or columns) and level.

Syntax:

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

Parameters

other: This is required. It indicates any single or multiple element data structure or list-like object

axis: This is optional. Choose between comparing by index (0 or ‘index’) or columns (1 or ‘columns’).

level: This is optional. To broadcast over a level, specify an int or a label that matches the Index values on the passed MultiIndex level. None is the default value.

Return Value:

The result of the comparison i.e, a boolean series is returned.

Pandas DataFrame ne() Function in Python

Example1

Here, the DataFrame is compared to a scalar value using the ne() function.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe
  • Apply ne() function to the dataframe by passing some random number to it and print the result.
  • Here it compares if the entire dataframe has values != 3(not equal to)
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 3, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply ne() function to the dataframe by passing some random number
# to it and print the result.
# Here it compares if the entire dataframe has values != 3
print("Checking if the entire dataframe has values != 3:")
print(data_frme.ne(3))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2              3
jessy                3             25
sindhu               4             90

Checking if the entire dataframe has values != 3:
        student_rollno  student_marks
virat             True           True
nick              True          False
jessy            False           True
sindhu            True           True

Example2

Here, the ‘other’ argument can be provided as a list to compare different columns with different scalar values

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply ne() function on the dataframe by passing list as an argument to it which compares the corresponding list elements with the dataframe and print the result.
  • Here it checks if the elements of the student_rollno column of the dataframe!=3 and student_marks column of the dataframe!=40.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 40, 25, 40]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply ne() function on the dataframe by passing list as an argument to it 
# which compares the corresponding list elements with the dataframe and print the result.
# Here it checks if the elements of the student_rollno column of the dataframe!=3
# and student_marks column of the dataframe!=40.
print("Checking if the student_rollno column!=3 and student_marks column!=40:")
print(data_frme.ne([3, 40]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             40
jessy                3             25
sindhu               4             40

Checking if the student_rollno column!=3 and student_marks column!=40:
        student_rollno  student_marks
virat             True           True
nick              True          False
jessy            False           True
sindhu            True          False

Example3

Here, ne() function is used on specific columns rather than the entire DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Apply ne() function on the specified column of dataframe by passing some random number to it and print the result.
  • Here it compares if the elements of student_rollno column!=3
  • Apply ne() function on the specified columns of the dataframe by passing list as an argument to it which compares the corresponding list elements with the specified columns of the dataframe and print the result.
  • Here it compares if the elements of the student_rollno column of the dataframe!=3 and student_marks column of the dataframe!=40.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 40, 25, 40]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply ne() function on the specified column of dataframe 
# by passing some random number to it and print the result.
# Here it compares if the elements of student_rollno column!=3
print("Checking if the student_rollno column has values !=3")
print(data_frme["student_rollno"].ne(3))
print()
# Apply ne() function on the specified columns of the dataframe by passing list
# as an argument to it which compares the corresponding list elements with
# the specified columns of the dataframe and print the result.
# Here it compares if the elements of the student_rollno column of the dataframe!=3
# and student_marks column of the dataframe!=40.
print("Checking if the student_rollno column!=3 and student_marks column!=40:")
print(data_frme[["student_rollno", "student_marks"]].ne([3, 40]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             40
jessy                3             25
sindhu               4             40

Checking if the student_rollno column has values !=3
virat      True
nick       True
jessy     False
sindhu     True
Name: student_rollno, dtype: bool

Checking if the student_rollno column!=3 and student_marks column!=40:
        student_rollno  student_marks
virat             True           True
nick              True          False
jessy            False           True
sindhu            True          False

Example4

In a DataFrame, the ne() method can be used to compare two series/column element-by-element for NOT equal to condition.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary) as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Compare both the columns gvn_list1 and gvn_list2 using the ne() function and store the result as a new column in the dataframe.
  • Here it checks if gvn_list1 != gvn_list2 element-wise.
  • Print the dataframe after adding a new column(gvn_list1 != gvn_list2).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary) as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme  = pd.DataFrame({
  "gvn_list1": [45, 7, 54, 90],
  "gvn_list2": [55, 7, 54, 44]
})
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Compare both the columns gvn_list1 and gvn_list2 using the ne() function
# and store the result as a new column in the dataframe.
# Here it checks if gvn_list1 != gvn_list2 element-wise.
data_frme ['gvn_list1 != gvn_list2'] = data_frme ['gvn_list1'].ne(data_frme ['gvn_list2'])
# Print the dataframe after adding a new column(gvn_list1 != gvn_list2)
print("The dataframe after adding a new column(gvn_list1 != gvn_list2):")
print(data_frme )

Output:

The given Dataframe:
   gvn_list1  gvn_list2
0         45         55
1          7          7
2         54         54
3         90         44

The dataframe after adding a new column(gvn_list1 != gvn_list2):
   gvn_list1  gvn_list2  gvn_list1 != gvn_list2
0         45         55                    True
1          7          7                   False
2         54         54                   False
3         90         44                    True

 

Leave a Comment