2

I try to obtain a 3D dataframe from 2 dataframes that have the same shape. If we take the below script as the example, I want to create a 3D dataframe that have 2 rows (a and b), 3 columns (A, B and C), and 2 items (df1 and df2). And also to be clear, I tried to explain my problem with a figure.

import pandas as pd
df1 = pd.DataFrame([[1, 2, 11], [3, 4, 12]], columns=['A', 'B', 'C'], index=['a', 'b'])
df2 = pd.DataFrame([[5, 6, 13], [7, 8, 14]], columns=['A', 'B', 'C'], index=['a', 'b'])

I saw related questions and tried some of them. I have tried to use pd.MultiIndex, but I obtained error as “ValueError: Length of levels and labels must be the same.”

Is there any way to obtain a 3D dataframe as described in below figure?

Thanks.

1

0

2 Answers 2

1

This library can be implemented to support this. I was not aware of this library's existence. This is the first time I installed it with this question and got the information from the official reference to create a response. Thank you for this opportunity.

import pandas as pd

df1 = pd.DataFrame([[1, 2, 11], [3, 4, 12]], columns=['A', 'B', 'C'], index=['a', 'b'])
df2 = pd.DataFrame([[5, 6, 13], [7, 8, 14]], columns=['A', 'B', 'C'], index=['a', 'b'])
ds1 = df1.to_xarray()

enter image description here

ds2 = df2.to_xarray()

enter image description here

import xarray as xr ds_all = xr.concat([ds1,ds2], dim='new_dim')

enter image description here

df_all = ds_all.to_dataframe()
df_all
        A   B   C
index   new_dim         
a   0   1   2   11
    1   5   6   13
b   0   3   4   12
    1   7   8   14
Sign up to request clarification or add additional context in comments.

Comments

0

Simply, you can do it like this.

import pandas as pd

df = pd.DataFrame(columns=['col'])

df1 = pd.DataFrame([[1, 2, 11], [3, 4, 12]], columns=['A', 'B', 'C'], index=['a', 'b'])
df2 = pd.DataFrame([[5, 6, 13], [7, 8, 14]], columns=['A', 'B', 'C'], index=['a', 'b'])

df = df.append({'col': df1}, ignore_index=True)
df = df.append({'col': df2}, ignore_index=True)

print(df)

enter image description here

print(df.iloc[1])

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.