Britt Tarien
Britt Tarien

Reputation: 21

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?

Upvotes: 2

Views: 532

Answers (1)

BENY
BENY

Reputation: 323226

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)]

Upvotes: 1

Related Questions