6.1 C
London
Saturday, December 21, 2024
HomePandas in PythonGeneral Functions in PythonHow to Create a 3D Pandas DataFrame (With Example)

How to Create a 3D Pandas DataFrame (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the xarray module to quickly create a 3D pandas DataFrame.

This tutorial explains how to create the following 3D pandas DataFrame using functions from the xarray module:

              product_A  product_B  product_C
year quarter                                 
2021 Q1        1.624345   0.319039         50
     Q2       -0.611756   0.319039         50
     Q3       -0.528172   0.319039         50
     Q4       -1.072969   0.319039         50
2022 Q1        0.865408  -0.249370         50
     Q2       -2.301539  -0.249370         50
     Q3        1.744812  -0.249370         50
     Q4       -0.761207  -0.249370         50

Example: Create 3D Pandas DataFrame

The following code shows how to create a 3D dataset using functions from xarray and NumPy:

import numpy as np
import xarray as xr

#make this example reproducible
np.random.seed(1)

#create 3D dataset
xarray_3d = xr.Dataset(
    {"product_A": (("year", "quarter"), np.random.randn(2, 4))},
    coords={
        "year": [2021, 2022],
        "quarter": ["Q1", "Q2", "Q3", "Q4"],
        "product_B": ("year", np.random.randn(2)),
        "product_C": 50,
    },
)

#view 3D dataset
print(xarray_3d)

Dimensions:    (year: 2, quarter: 4)
Coordinates:
  * year       (year) int32 2021 2022
  * quarter    (quarter) 

Note: The NumPy randn() function returns sample values from the standard normal distribution.

We can then use the to_dataframe() function to convert this dataset to a pandas DataFrame:

#convert xarray to DataFrame
df_3d = xarray_3d.to_dataframe()

#view 3D DataFrame
print(df_3d)

              product_A  product_B  product_C
year quarter                                 
2021 Q1        1.624345   0.319039         50
     Q2       -0.611756   0.319039         50
     Q3       -0.528172   0.319039         50
     Q4       -1.072969   0.319039         50
2022 Q1        0.865408  -0.249370         50
     Q2       -2.301539  -0.249370         50
     Q3        1.744812  -0.249370         50
     Q4       -0.761207  -0.249370         50

The result is a 3D pandas DataFrame that contains information on the number of sales made of three different products during two different years and four different quarters per year.

We can use the type() function to confirm that this object is indeed a pandas DataFrame:

#display type of df_3d
type(df_3d)

pandas.core.frame.DataFrame

The object is indeed a pandas DataFrame.

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

Pandas: How to Find Unique Values in a Column
Pandas: How to Find the Difference Between Two Rows
Pandas: How to Count Missing Values in DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories