简体   繁体   中英

iterating through different pandas dataframe names

I have a 17 dataframes with similar names (df1, df2, df3,...) and would like to be able to write a for loop that will perform the same operations on each of the dataframes.

    df1 = pd.read_csv("filename1")
    df2 = pd.read_csv("filename2")
    ...
    df17 = pd.read_csv("filename17")

    for i in range (1,17):
        "operations"

How can I iterate through the names of these dataframes in the for loop?

Just you can put them into list as user3483203 mentioned at comment , then we using pd.concat with keys , afther that we using groupby to implement your function

l=[df1,df2...]
alldf=pd.concat(l,keys=list(range(len(l))))
allldf=alldf.groupby(level=0).apply('your function')

After the result we can using groupby split the data frame again

[x for _,x in alldf.groupby(level=0)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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