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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With