Reshaping Your Data for Efficient DataFrame Creation: A Step-by-Step Guide

The issue is that results is a list of lists, and you’re trying to create a DataFrame from it. When you use zip(), it creates an iterator that aggregates the values from each element in the lists into tuples, which are then converted to Series when creating the DataFrame.

To achieve your desired format, you need to reshape the data before creating the DataFrame. You can do this by using the values() attribute of each model’s value accessor to get the values as a 2D array, and then using pd.DataFrame with the columns specified in order.

Here is an example:

import pandas as pd

# assuming Date, Ein, Eout, Z, NES, L are your variables

Date = [2019-01-01] * len(model.Ein.get_values().values())
Ein = model.Ein.get_values().values()
Eout = model.Eout.get_values().values()
Z = model.Z.get_values().values()
NES = model.NES.get_values().values()
L = model.L.get_values().values()

df_results = pd.DataFrame({
    'Date': Date,
    'Ein': Ein,
    'Eout': Eout,
    'Z': Z,
    'NES': NES,
    'L': L
})

This will create a DataFrame with the desired columns and shape. Note that I assume model.Ein.get_values().values(), etc., return 1D arrays or lists, so I use * len() to repeat the value for each row. If they are already in the correct shape (2D), you can skip this step.

Alternatively, if your models’ value accessors return data frames or Series with the desired structure, you can simply assign them directly:

df_results = pd.DataFrame({
    'Date': model.Date,
    'Ein': model.Ein.get_values(),
    'Eout': model.Eout.get_values(),
    'Z': model.Z.get_values(),
    'NES': model.NES.get_values(),
    'L': model.L.get_values()
})

This assumes that model.Date is a Series with the correct values, and model.Ein, etc., are Series or DataFrames with the desired structure.


Last modified on 2023-07-06