Mar-18-2020, 09:31 AM
dict_of_results = {} data = pd.read_csv('data.csv', sep = ';', index_col = False) # data.csv looks like the following, containing 2 objects: # parameter1;parameter2;parameter3 # 388;2000;14 # 293;1890;12.6 list_of_objects = [list(row) for row in data.values] for item in range(len(list_of_objects)): ElectricityDemand = [] FuelDemand = [] MyObject = Class(parameter1 = list_of_objects[item][0], parameter2 = list_of_objects[item][1], parameter3= list_of_objects[item][2]) # Load more datafiles and define some constants # Iterating over every hour of a year and calculate the ElectricityDemand and FuelDemand of the object for h in range(8760): # ...calculate something here... then store the results in the lists below ElectricityDemand.append(MyObject.electricity_demand) FuelDemand.append.append(MyObject.fuel_demand) results = pd.DataFrame({'ElectricityDemand': ElectricityDemand, 'FuelDemand': FuelDemand}) for i in range(len(list_of_objects)): results['iter'] = i dict_of_results[i] = results.copy()This programm iterates over the hours of a year (8760 hours) - see inner loop - for 2 different objects (see outer loop: range(len(list_of_objects))).
I'd like to store the calculation results of the inner loop in lists (ElectricityDemand and FuelDemand), then in a (temporary) DataFrame (results) and then store the DataFrame in a dictionary, afterwards continuing with the same procedure for object 2.
At this stage, I get a dictionary with 2 DataFrames each having 8760 observations but both DataFrames with the calculation results for object 2 (last iteration of the for loop).
My output now:
dict_of_results
Type: dict
Size: 3
Value: {0:DataFrame, 1:DataFrame}
in 0:DataFrame
Type: DataFrame
Size: (8760, 3)
Value: Column names: ElectricityDemand and FuelDemand as well as iter
and 8760 rows for ElectricityDemand and FuelDemand (from object 2 (from input data 293;1890;12.6)) as well as iter = 0 for each row, which is correct.
in 1:DataFrame
Excactly the same as in 0:DataFrame, but the value for iter changed to 1 which is again correct.
My desired output:
dict_of_results
Type: dict
Size: 3
Value: {0:DataFrame, 1:DataFrame}
in 0:DataFrame
Type: DataFrame
Size: (8760, 3)
Value: Column names: ElectricityDemand, FuelDemand and iter
and 8760 rows for object one (from input data 388;2000;14) as well as iter = 0 for each observation
in 1:DataFrame
Also size (8760, 3), ElectricityDemand and FuelDemand with the corresponding rows/values/results for the second object (from input data 293;1890;12.6) as well as iter = 1 in each row.