Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create utility for putting 3D objects into Pandas DataFrames #1009

Open
jdebacker opened this issue Mar 11, 2025 · 0 comments
Open

Create utility for putting 3D objects into Pandas DataFrames #1009

jdebacker opened this issue Mar 11, 2025 · 0 comments

Comments

@jdebacker
Copy link
Member

For those looking to use the model output in other software (e.g., for plotting), it can be helpful to be able to put three dimensional objects into a DataFrame as panel dataset.

We could add a utility to utils.py to simplify this. The code could be something like

def D3_to_df(tpi_vars, var="c_path", start_year=2025):

    data = tpi_vars[var]
    # First, get the dimensions of your array
    T, S, J = data.shape

    # Create index values for each dimension
    idx_t = [f'{start_year + i}' for i in range(T)]  # You can customize these labels
    idx_s = [f'{i}' for i in range(S)]
    idx_j = [f'{i}' for i in range(J)]

    # Create MultiIndex from the first two dimensions
    multi_idx = pd.MultiIndex.from_product([idx_t, idx_s], names=['Year', 'Age'])

    # Reshape the 3D array to 2D
    reshaped_data = data.reshape(T * S, J)

    # Create the DataFrame with MultiIndex
    df = pd.DataFrame(reshaped_data, index=multi_idx, columns=idx_j)

    return df

Then use this function with:

df = D3_to_df(tpi_vars_bas, var="c_path", start_year=p_bas.start_year)
df.to_csv("c_path.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant