Python pd using a variable with column name in groupby dot notation

Multi tool use
Python pd using a variable with column name in groupby dot notation
I am trying to use a list that holds the column names for my groupby notation. My end goal is to loop through multiple columns and run the calculation without having to re-write the same line multiple times. Is this possible?
a_list = list(['','BTC_','ETH_'])
a_variable = ('{}ClosePrice'.format(a_list[0]))
proccessing_data['RSI'] = proccessing_data.groupby('Symbol').**a_variable**.transform(lambda x: talib.RSI(x, timeperiod=14))
this is the error I currently get because it thinks I want the column 'a_variable' which doesn't exist.
AttributeError: 'DataFrameGroupBy' object has no attribute 'a_variable'
df[a_variable]
df.a_variable
the name of the library is pandas. Please tag carefully next time. the [pd] tag is for Placement Driver.
– juanpa.arrivillaga
Jul 1 at 18:28
1 Answer
1
Apparently this notation below works:
proccessing_data['RSI'] = proccessing_data.groupby('Symbol')[('{}ClosePrice'.format(a_list[0]))].transform(lambda x: talib.RSI(x, timeperiod=14))
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
use
df[a_variable]
instead ofdf.a_variable
– RafaelC
Jul 1 at 18:16