Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to self in pandas subsetting

When I'm exploring data in an ad hoc way, I often have code like this:

X = (adj_all.o.diff(1) / adj_none.o.diff(1)).diff(1)
print(X[X > 0])

Is there a way to do this in a single line in an easy way? The following works but is verbose:

(adj_all.o.diff(1) / adj_none.o.diff(1)).diff(1)[(adj_all.o.diff(1) / adj_none.o.diff(1)).diff(1) > 0]

I want something like this:

(adj_all.o.diff(1) / adj_none.o.diff(1)).diff(1)[self > 0]

Note that this isn't production code. It is part of ad hoc exploration where iteration speed is important to results, which is why I wish to be able to do this common thing in a single line.

like image 636
Jase Avatar asked Sep 17 '25 14:09

Jase


1 Answers

One of the many uses of .loc is exactly this. You can pass it a lambda function, which will take the dataframe as a parameter and return a mask to filter by:

(adj_all.o.diff(1) / adj_none.o.diff(1)).diff(1).loc[lambda x:x>0]