pandas DataFrame Comparision method - DataFrame.ne()
In this pandas tutorial we will discuss about pandas ne function or ne() comparison method.
Introduction
DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key.
Example: Create Pandas dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with integers data and assign indices through index parameter.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: DataFrame is created below
column1 column2 column3 column4
one 100 100 23 1234
two 200 200 45 54
three 300 300 67 67
four 200 200 43 8
DataFrame.ne()
This method (pandas ne function) is used to check the given value is not equal to elements present in the dataframe.
If present, It will return False at that element position, otherwise it will return True. It is quite opposite to eq() function
Syntax:
dataframe_input.ne(value)
where, dataframe_input is the input pandas dataframe.
Example:
In this pandas ne function example, we will create a dataframe and check the ne() method functionality with two scenarios.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#check the elements present in the dataframe are not equal to 200
print(data.ne(200))
print()
#check the elements present in the dataframe are not equal to 23
print(data.ne(23))
Output:
In the first output, we observed that in index - two : column 1 and column 2 contains 200. so the result is False and in index - four: column 1 and column 2 contains 200. so the result is false and remaining are set to True.
Similarly, in the second output, we will check with value - 23.
column1 column2 column3 column4
one True True True True
two False False True True
three True True True True
four False False True True
column1 column2 column3 column4
one True True False True
two True True True True
three True True True True
four True True True True
We can also place an operator instead as pandas ne function. The operator used is "!=".
Syntax:
dataframe_input!=value
Example:
In this pandas ne function example, we will create a dataframe and check the "!=" operator functionality with two scenarios.
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#check the elements present in the dataframe are not equal to 200
print(data!=200)
print()
#check the elements present in the dataframe are not equal to 23
print(data!=23)
Output:
In the first output, we observed that in index - two : column 1 and column 2 contains 200. so the result is False and in index - four: column 1 and column 2 contains 200. so the result is false and remaining are set to True.
Similarly, in the second output, we will check with value - 23.
column1 column2 column3 column4
one True True True True
two False False True True
three True True True True
four False False True True
column1 column2 column3 column4
one True True False True
two True True True True
three True True True True
four True True True True
We can also compare with column wise by providing different values.
Note - The total values must be equal to column count
Syntax:
dataframe_input.ne([values])
Example:
In this pandas ne function example, we will compare the dataframe with 4 values (since number of columns are 4).
import pandas as pd
#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],
'column2':[100,200,300,200],
"column3":[23,45,67,43],
"column4":[1234,54,67,8]
},index=['one','two','three','four'])
#display actual dataframe
print(data)
print()
#check the elements present in the dataframe are not equal to the given values in list or not
print(data.ne([100, 200, 23,8]))
Output:
From the above example, the first value - 100 is compared with first column and set to False whereever 100 is present, the second value - 200 is compared with second column and set to False whereever 200 is present, the third value - 23 is compared with third column and set to False whereever 23 is present and the last value - 8 is compared with forth/last column and set to False whereever 8 is present. Remaining all are set to True..
column1 column2 column3 column4
one 100 100 23 1234
two 200 200 45 54
three 300 300 67 67
four 200 200 43 8
column1 column2 column3 column4
one False True False True
two True False True True
three True True True True
four True False True False
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Apr 22,2023