Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas DataFrame.eval function to alter subset of rows inplace

I know this works in pandas (df is a dataframe, op is a column, mult is a float variable):

df.eval("op = op * @mult", inplace=True)

But is it possible to do it on a subset of rows (in place)? This gives me an error (ex_date is a local variable of type timestamp, and the index of df is a timestamp):

df.eval("df.loc[df.index < @ex_date, op] = op * @mult", inplace=True)

Error is: SyntaxError: left hand side of an assignment must be a single name

like image 305
techvslife Avatar asked Feb 07 '17 00:02

techvslife


1 Answers

It is not currently possible to do a conditional eval in place. There is an outstanding feature request for this. The proposed syntax would have your example looking something like:

df.eval("op = op * @mult if index < @ex_date else op", inplace=True)

(SOURCE)

like image 146
Stephen Rauch Avatar answered Oct 11 '22 19:10

Stephen Rauch