Skip to content Skip to sidebar Skip to footer

Iterating Over Several Dataframes Generated Using "locals" : Python

I have split a dataframe 'df' into smaller dataframes df1, df2...dfn such that all records with the same ID (from column 'UNIT-ID') are grouped together and stored in those smaller

Solution 1:

You could try something like iterating through all your code variables, selecting by name the ones who are your "sub-dataframes" (for example, using a pattern in their names such as subDf) and them executing something just in those variables. To make my idea more clear, run the example below:

variables = locals()
for i,j inenumerate(df.groupby('UNIT-ID')):
    variables["subDf{0}".format(i+1)] = j[1]

for each in [v for k,v in variables.items() if'subDf'in k]:
    print(v)

#output:#   UNIT-ID  Q1   Q2  Q3#6   110-15  23  346   0#7   110-15  31  419   1#8   110-15  37  287   0#9   110-15  36  228   1#10  110-15  48  309   1#  UNIT-ID  Q1   Q2  Q3#0  110-P1  37  487   0#1  110-P1  31  140   1#2  110-P1  46  214   1#  UNIT-ID  Q1   Q2  Q3#3  110-P2  29  287   1#4  110-P2  45  131   1#5  110-P2  39  260   0

This way, you can print all the subdataframes without having to save them elsewhere. Since I'm unsure what exactly you plan to do with your data, I can't tell if this is the best approach. But will definitely iterate through the dataframes you created!

Post a Comment for "Iterating Over Several Dataframes Generated Using "locals" : Python"