2

R has with() and within() functions, which allow you to refer to the columns of a data.frame without prepending the name of the data.frame. This can be useful to avoid some keystrokes and make statements simpler.

Is there something similar to this in pandas?

1 Answer 1

7

There's nothing exactly equivalent (you can't attach the DataFrame to the namespace like in R), but you can use the experimental DataFrame.query() method and DataFrame.eval() method, available in 0.13 (release candidate should be out this week), will let you do things like:

>>> df = DataFrame({"A": range(10), "B": range(0, 20, 2)}) >>> df A B 0 0 0 1 1 2 2 2 4 3 3 6 4 4 8 5 5 10 6 6 12 7 7 14 8 8 16 9 9 18 >>> df.query("A < 5 and B in (1, 2, 3, 5, 6)") A B 1 1 2 3 3 6 >>> df.eval("A + B") 0 0 1 3 2 6 3 9 4 12 5 15 6 18 7 21 8 24 9 27 dtype: int64 

As a bonus, on large frames this gets accelerated by numexpr.

The statsmodels' formula API also lets you refer to formulas like in R.

Sign up to request clarification or add additional context in comments.

2 Comments

There's actually a perf comparison with with and subset lying around the repo somewhere too...can't remember exactly where I put it. subset -> DataFrame.query() and with -> DataFrame.eval() is the way I was thinking about how the pandas methods relate to the corresponding R versions.
So, technically, DataFrame.eval() is equivalent, since it takes an arbitrary expression and evaluates it in the context of the DataFrame instance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.