How to save multiple dataframe using one variable in a for loop
Question:
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.
Answers:
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
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!
Thank you Guys,
number = 1
for i in range(0,7):
for k in range(0,4):
for m in range(0,6):
for n in range(0,11):
case = data[(data["CURRENT_ENERGY_RATING_Code"] == i)&(data["PROPERTY_TYPE_Code"] == k)&(data["BUILT_FORM_Code"] == m)&(data["CONSTRUCTION_AGE_BAND_Code"] == n)]
case.to_csv(f"simulAll_Parameterssimul_{number}.csv")
number += 1
This Code work’s for me, I saved the output in a local folder
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.
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
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!
Thank you Guys,
number = 1
for i in range(0,7):
for k in range(0,4):
for m in range(0,6):
for n in range(0,11):
case = data[(data["CURRENT_ENERGY_RATING_Code"] == i)&(data["PROPERTY_TYPE_Code"] == k)&(data["BUILT_FORM_Code"] == m)&(data["CONSTRUCTION_AGE_BAND_Code"] == n)]
case.to_csv(f"simulAll_Parameterssimul_{number}.csv")
number += 1
This Code work’s for me, I saved the output in a local folder