How to Create a 3D Pandas DataFrame

To create a 3D Pandas DataFrame, you can use a combination of the pd.Panel and pd.DataFrame objects.

Using pd.Panel and pd.DataFrame

import pandas as pd
import numpy as np

data = np.random.randn(2, 3, 4)

panel = pd.Panel(data)

df = panel.to_frame()

print(df)

In this example, we first create a 3D numpy array using the np.random.randn function. We then create a Pandas panel object using this numpy array, and convert it to a 3D DataFrame using the to_frame() method. Finally, we print the resulting DataFrame.

import pandas as pd
import numpy as np

data = np.random.randn(2, 3, 4)

idx = pd.MultiIndex.from_product([range(s) for s in data.shape], names=['x', 'y', 'z'])

df = pd.DataFrame({'value': data.flatten()}, index=idx)

print(df)

In this example, we first create a 3D numpy array as before. We then create a MultiIndex using the pd.MultiIndex.from_product method, specifying the range of values for each dimension and giving them appropriate names. Finally, we create a DataFrame with the flattened numpy array and the MultiIndex, which gives us a 3D DataFrame.

Using pd.MultiIndex

This method involves creating a Pandas DataFrame with a MultiIndex, where each level of the MultiIndex corresponds to a dimension of the 3D DataFrame.

import pandas as pd
import numpy as np

data = np.random.randn(2, 3, 4)

idx = pd.MultiIndex.from_product([range(s) for s in data.shape], names=['x', 'y', 'z'])

df = pd.DataFrame({'value': data.flatten()}, index=idx)

print(df)

Which method you choose depends on your personal preference and the specific needs of your project. The pd.Panel method is more straightforward, but the pd.MultiIndex method gives you more flexibility in how you index the 3D DataFrame.

This Post Has 2 Comments

Leave a Reply