Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Python Pandas - Panel



Apanel is a 3D container of data. The termPanel data is derived from econometrics and is partially responsible for the name pandas −pan(el)-da(ta)-s.

The Panel class is deprecated and has been removed in recent versions of pandas. The recommended way to represent 3-D data is with a MultiIndex on a DataFrame via the to_frame() method or with the xarray package. pandas provides a to_xarray() method to automate this conversion.

The names for the 3 axes are intended to give some semantic meaning to describing operations involving panel data. They are −

  • items: axis 0, each item corresponds to a DataFrame contained inside.

  • major_axis: axis 1, it is the index (rows) of each of the DataFrames.

  • minor_axis: axis 2, it is the columns of each of the DataFrames.

pandas.Panel()

A Panel 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 −

ParameterDescription
dataData takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame
itemsaxis=0
major_axisaxis=1
minor_axisaxis=2
dtypeData type of each column
copyCopy data. Default,false

Create Panel

A Panel can be created using multiple ways like −

  • From ndarrays
  • From dict of DataFrames

From 3D ndarray

# creating an empty panelimport pandas as pdimport numpy as npdata = np.random.rand(2,4,5)p = pd.Panel(data)print(p)

Itsoutput is as follows −

<class 'pandas.core.panel.Panel'>Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)Items axis: 0 to 1Major_axis axis: 0 to 3Minor_axis axis: 0 to 4

Note: Observe the dimensions of the empty panel and the above panel, all the objects are different.

From dict of DataFrame Objects

#creating an empty panelimport pandas as pdimport numpy as npdata = {'Item1' : pd.DataFrame(np.random.randn(4, 3)),    'Item2' : pd.DataFrame(np.random.randn(4, 2))}p = pd.Panel(data)print(p)

Itsoutput is as follows −

Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)Items axis: Item1 to Item2Major_axis axis: 0 to 3Minor_axis axis: 0 to 2

Create an Empty Panel

An empty panel can be created using the Panel constructor as follows −

#creating an empty panelimport pandas as pdp = pd.Panel()print(p)

Itsoutput is as follows −

<class 'pandas.core.panel.Panel'>Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)Items axis: NoneMajor_axis axis: NoneMinor_axis axis: None

Selecting the Data from Panel

Select the data from the panel using −

  • Items
  • Major_axis
  • Minor_axis

Using Items

# creating an empty panelimport pandas as pdimport numpy as npdata = {'Item1' : pd.DataFrame(np.random.randn(4, 3)),    'Item2' : pd.DataFrame(np.random.randn(4, 2))}p = pd.Panel(data)print(p['Item1'])

Itsoutput is as follows −

            0          1          20    0.488224  -0.128637   0.9308171    0.417497   0.896681   0.5766572   -2.775266   0.571668   0.2900823   -0.400538  -0.144234   1.110535

We have two items, and we retrieved item1. The result is a DataFrame with 4 rows and 3 columns, which are theMajor_axis andMinor_axis dimensions.

Using major_axis

Data can be accessed using the methodpanel.major_axis(index).

# creating an empty panelimport pandas as pdimport numpy as npdata = {'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))

Itsoutput is as follows −

      Item1       Item20   0.417497    0.7484121   0.896681   -0.5573222   0.576657       NaN

Using minor_axis

Data can be accessed using the methodpanel.minor_axis(index).

# creating an empty panelimport pandas as pdimport numpy as npdata = {'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))

Itsoutput is as follows −

       Item1       Item20   -0.128637   -1.0470321    0.896681   -0.5573222    0.571668    0.4319533   -0.144234    1.302466

Note: Observe the changes in the dimensions.

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp