0

I am new to Python and would like to ask for help using for loops

I have a dataframe of stock codes and prices which I have split using the groupby function into the respective stock codes as per below

grouped = list(dft.groupby(dft.StockCode))

What I'm trying to do is a for loop that iterates over the list of dataframes to create a new variable in each dataframe. For example, I would like to calculate the RSI for each stock code.

for df in grouped:
    df['RSI']=ta.RSI(df['Close'],14)

I could do this individually by splitting out each stock code and then calculating it but it would be a very time consuming process and I'm wondering if there is a more elegant solution.

Thanks in advance!

cirrus
  • 25
  • 2

1 Answers1

0

What about this?

dft_new = dft.groupby('StockCode').apply(lambda x: ta.RSI(x, 14))

I haven't tested it. But the idea is to use pandas apply on groups.

samusa
  • 449
  • 2
  • 11