Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas SQL equivalent for 'not equal' clause

Tags:

python

sql

pandas

I do not see this in the SQL comparison documentation for Pandas. What would be the equivalent of this SQL in Pandas?

select a.var1, a.var2, b.var1, b.var2
from tablea a, tableb b
where a.var1=b.var1 
and a.var2=b.var2
and a.var3 <> b.var3

I have the merge code as follows:

df = pd.merge(a, b, on=['VAR1','VAR2'], how='inner')

How do I incorporate the 'not equal' portion?

and a.var3 <> b.var3
like image 601
P Spence Avatar asked Aug 28 '17 21:08

P Spence


People also ask

How do you write not equal to in SQL query?

The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 !=

Is Panda faster than SQL?

This main difference can mean that the two tools are separate, however, you can also perform several of the same functions in each respective tool, for example, you can create new features from existing columns in pandas, perhaps easier and faster than in SQL.

Can I use SQL in pandas?

Pandasql is a python library that allows manipulation of a Pandas Dataframe using SQL. Under the hood, Pandasql creates an SQLite table from the Pandas Dataframe of interest and allow users to query from the SQLite table using SQL.


1 Answers

You can query the resulting frame:

a.merge(b, on=['VAR1','VAR2']).query('VAR3_x != VAR3_y')
like image 156
ayhan Avatar answered Nov 06 '22 07:11

ayhan