0

let's say i have multiple dataframe df1, df2,...,df100 and I have function as written below. I don't know how to apply the function to multiple (a hundred df) in shorter way. any suggestion? many thanks,

def sma(x,length):
   ma = x.rolling(window=length,min_periods=1).mean()
   return (x / ma) -1

sma(df1, 40)
stvlam22
  • 63
  • 7
  • use a list/dictionary to store all dataframes and use: `out = [sma(d, 40) for d in list_of_dfs]` or `out = {k: sma(d, 40) for k, d in dic_of_dfs.items()}` – mozway Nov 24 '22 at 13:38
  • okay thanks mozway, after that i just brekadown the list into different dataframe – stvlam22 Nov 24 '22 at 14:08
  • You're welcome. I really recommend you to use a dictionary, thus you don't have to manually handle hundred variables! – mozway Nov 24 '22 at 14:14
  • Hi mozway, can you give me a simple example to use a dictionary relating to the topic ? or any reference link , so i can study it by myself, thanks in advance – stvlam22 Nov 25 '22 at 02:58
  • `dfs = {'A': df1, 'B': df2} ; out = {k: sma(d, 40) for k, d in dfs.items()}` – mozway Nov 25 '22 at 06:08
  • it works much better.. thanks mozway – stvlam22 Nov 25 '22 at 07:04

0 Answers0