简体   繁体   English

如何使用多个数据集运行1个函数?

[英]How to run 1 function with multiple dataset?

Assuming I got 20 set of data which I read it as df1,df2,df3,df4...df20. 假设我有20组数据,我将其读取为df1,df2,df3,df4 ... df20。 (Any other smart way to read or store dataframe?) (还有其他读取或存储数据框的智能方式吗?)

my function be like: 我的功能是这样的:

def CalculateEMA(x,window):    
    sma = x.rolling(window, min_periods=window).mean()[:window]
    rest = x[window:]
    EMA_window=(pd.concat([sma, rest]).ewm(span=window,adjust=False).mean()).sum())

    return EMA_window

And I wish to return a EMA for all 20 data frame and store as X . 我希望返回所有20个数据帧的EMA并存储为X。 So my final output will be X = [x1,x2,x3,x,x,x,x,x...x20]. 所以我的最终输出将是X = [x1,x2,x3,x,x,x,x,x ... x20]。 Where x1 is the Sum of EMA fucntion for df1 and x2 is second dataset df2 and so on. 其中X1是EMA的总和的温控功能的DF1和X2是第二个数据集DF2等。

You can loop throughout your dataframes if you stock them in a list : 如果将它们存储在列表中,则可以遍历整个数据框:

X=list()
list_df = [df1,df2,...,df20]
for df in list_df:
    X.append(CalculateEMA(df,window))

to read all the csv files in a folder you can make use of os function 读取文件夹中的所有csv文件,您可以使用os函数

    import os
    path_to_dir='<your path>'
    filenames = os.listdir(path_to_dir)
    csvs=[f for f in filenames if f.endswith('csv')]
    out=[]
    for path in csvs:
        df=pd.read_csv(path)
        out.append(CalculateEMA(df,window))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM