English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Basic operations of Pandas Panel
Panel data3D container. Term Panel data Originating from econometrics, the name comes from pandas − pan(el)-da(ta)-s.
3The names of the axes are described as follows- −
items − Axis 0, each item corresponds to a DataFrame contained within.
major_axis − Axis1which is each DataFrame's index (row).
minor_axis − Axis2which is each DataFrame's column.
Panels can be created using the following constructor:- −
pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
The parameters of the constructor are as follows:
Parameters | Description |
data | Data can take various forms, such as ndarray, series, map, list, dict, constants, and DataFrame |
items | axis=0 |
major_axis | axis=1 |
minor_axis | axis=2 |
dtype | Data type of each column |
copy | Copy data. Default false |
Panels can be created in various ways, such as:
Create from ndarrays Create from DataFrame dictionary
# Create an empty panel import pandas as pd import numpy as np data = np.random.rand(2,4,5) p = pd.Panel(data) print(p)
The running result is as follows:
<class 'pandas.core.panel.Panel'> Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis) Items axis: 0 to 1 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 4
# Create an empty panel import pandas as pd import numpy as np data = { 'Item1': pd. DataFrame(np. random.randn(4, 3)) 'Item2': pd. DataFrame(np. random.randn(4, 2))} p = pd. Panel(data) print(p)
Running Results:
Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis) Items axis: Item1 to Item2 Major_axis axis: 0 to 3 Minor_axis axis: 0 to 2
A Panel can be created using the Panel constructor function, as shown below:
# Create an empty panel import pandas as pd p = pd.Panel() print(p)
Running Results:
<class 'pandas.core.panel.Panel'> Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis) Items axis: None Major_axis axis: None Minor_axis axis: None
You can query data from the panel using the following three items:
Items Major_axis Minor_axis
# Create an empty panel import pandas as pd import numpy as np data = {
Running Results:
0 1 2 0 0.488224 -0.128637 0.930817 1 0.417497 0.896681 0.576657 2 -2.775266 0.571668 0.290082 3 -0.400538 -0.144234 1.110535
Query item from two items1, the output is a DataFrame with4Rows3Columns' DataFrame, respectively Major_axis and Minor_axis.
You can access the data using the panel.major_axis(index) method.
# Create an empty panel import pandas as pd import numpy as np data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)) 'Item2' : pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print(p.major_xs(1))
Running Results:
Item1 Item2 0 0.417497 0.748412 1 0.896681 -0.557322 2 0.576657 NaN
You can access the data using the panel.minor_axis(index) method.
# Create an empty panel import pandas as pd import numpy as np data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)) 'Item2' : pd.DataFrame(np.random.randn(4, 2))} p = pd.Panel(data) print(p.minor_xs(1))
Running Results:
Item1 Item2 0 -0.128637 -1.047032 1 0.896681 -0.557322 2 0.571668 0.431953 3 -0.144234 1.302466