ByRequest
ByRequest

Reputation: 311

Filtering unlike columns in Pandas Dataframe

I have a dataframe "Query1".

I want to filter this dataframe into a dataframe containing only rows which have two columns that are not equal to each other. The data types in both of these columns are strings (most of the strings are numeric).

With the following operation(s), "Test" returns a series of booleans for each row (as expected) indicating true/false for the match.

Test = Query1['Sponsor ID'] != Query1['Sponsor Number']  
Test = Query1['Sponsor ID'] != ['Sponsor Number']

It seems from here, I should be able to run something like this to filter on the condition:

Output = Query1[Query1['Sponsor ID'] != Query1['Sponsor Number']]
Output = Query1[[Query1['Sponsor ID'] != Query1['Sponsor Number']]==True]
Output = Query1[Query1['Sponsor ID'] != Query1['Sponsor Number']==True]
Output = Query1.loc[Query1['Sponsor ID'] != Query1['Sponsor Number']]

However, these four tests fail and either error out, or return all rows without filtering or even including the boolean.

I attempted testing the boolean on it's own to ensure it is returning the eleven values I am expecting in the dataframe, however, it looks like I have something wrong in my syntax. The "correct" syntax to my knowledge should be something along the lines of df1[‘col1’] == value. Trying the following appears to set my boolean to either "True" or "False" on every row.

print(Test[1:]==False)
print(Test[1:1]==True)
print(Test.loc[1:]==False)

Upvotes: 2

Views: 393

Answers (1)

Nikaido
Nikaido

Reputation: 4629

Try in this way:

Query1.loc[~(Query1['Sponsor ID'] == Query1['Sponsor Number'])]

~ works as complementary (like not)

Upvotes: 1

Related Questions