Home > Net >  How to save multiple dataframe using one variable in a for loop
How to save multiple dataframe using one variable in a for loop

Time:09-13

import numpy as np

count = np.arange(0,1849)
for i in range(0,6):   
  for j in range (0,6):
    for k in range (0,4):
        for l in range (0,10):
            for m in count:
              case = data[(data["CURRENT_ENERGY_RATING_Code"] == i)&(data["PROPERTY_TYPE"] == j)&(data["BUILT_FORM"] == k)&(data["CONSTRUCTION_AGE_BAND"] == l)]
              case[m] = pd.DataFrame()

I wanted to save multiple data frames within the case variable with a proper number like case1, case2, etc. So I can view each data frame.

CodePudding user response:

You could create a dictionary with keys in the format i-j-k-l-m iterating through 0,1,2,etc; and values as the relevant dataframes. For example:

dic = {}
count = np.arange(0,1849)
for i in range(0,6):   
    for j in range (0,6):
        for k in range (0,4):
            for l in range (0,10):
                for m in count:
                    key = str(i)   '-'   str(j)   '-'   str(k)   '-'   str(l)   '-'   str(m)
                    dic[key] = data[(data["CURRENT_ENERGY_RATING_Code"] == i)&(data["PROPERTY_TYPE"] == j)&(data["BUILT_FORM"] == k)&(data["CONSTRUCTION_AGE_BAND"] == l)]
print(dic)
#note resulting `dic` is a huge dictionary!

CodePudding user response:

Created names conflict with variables already used by your logic, so you need to use all variables (i, j, k, l, m) or a counter like this:

d = {}
number = 1

d['case'   str(number)] = pd.DataFrame()
number  = 1
  • Related