syntax.us
Let the syntax do the talking

Blog Contact Posts Questions Tags Hire Me 
Question:
In Pandas how do I apply WHERE?
I have a Python Pandas DataFrame which looks like this:
If these rows were in Sqlite I could do this SELECT:
Then, Sqlite would return the last 4 rows.
How do I return the last 4 rows using Pandas? When I use Pandas for this type of task, I first focus my mind on what I call the 'predicate'. In this example the predicate is this:
A proper predicate returns a Boolean.
The above predicate works well in SQL. How do I write it in Pandas? Answer:
Now that I have my predicate, I can apply it to my Pandas DataFrame:
And then I can see something like this:
So now I have a column full of Booleans.
A common idea in Pandas, NumPy, and R is to apply a column full of Booleans to data shaped like a table. If I match a True to a row in the table, I get that row. If I match a False to a row in the table, I get nothing. The way I do this in Pandas is to place the Booleans on the right hand side. This is easy (for me) to remember because in SQL, predicates also go on the right hand side. Let the syntax do the talking:

syntax.us Let the syntax do the talking 
Blog Contact Posts Questions Tags Hire Me 