Python Pandas 简明教程

Python Pandas - Panel

panel 是数据的 3D 容器。术语 Panel data 衍生自计量经济学并且部分负责熊猫名称 − pan(el)-da(ta) -s。

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

3 根轴的名称是为了给描述面板数据的操作赋予一些语义含义。它们是 −

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

  1. items − axis 0, each item corresponds to a DataFrame contained inside.

  2. major_axis − axis 1, it is the index (rows) of each of the DataFrames.

  3. 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 −

Parameter

Description

data

Data takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame

items

axis=0

major_axis

axis=1

minor_axis

axis=2

dtype

Data type of each column

copy

Copy data. Default, false

Create Panel

可以使用多种方法创建面板,如 −

A Panel can be created using multiple ways like −

  1. From ndarrays

  2. From dict of DataFrames

From 3D ndarray

# creating an empty panel
import pandas as pd
import numpy as np

data = np.random.rand(2,4,5)
p = pd.Panel(data)
print p

它的 output 如下所示 −

Its output 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

Note − 观察空面板和上述面板的维度,所有对象都不同。

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 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

它的 output 如下所示 −

Its output is as follows −

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

Create an Empty Panel

可以使用 Panel 构造函数创建空面板,如下所示 −

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

#creating an empty panel
import pandas as pd
p = pd.Panel()
print p

它的 output 如下所示 −

Its output is as follows −

<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

Selecting the Data from Panel

使用以下方法从面板中选择数据 −

Select the data from the panel using −

  1. Items

  2. Major_axis

  3. Minor_axis

Using Items

# creating 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['Item1']

它的 output 如下所示 −

Its output is as follows −

            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

我们有两项,并且我们检索了 item1。结果是一个带有 4 行和 3 列的 DataFrame,它们是 Major_axisMinor_axis 维度。

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

Using major_axis

可以使用方法 panel.major_axis(index) 访问数据。

Data can be accessed using the method panel.major_axis(index).

# creating 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)

它的 output 如下所示 −

Its output is as follows −

      Item1       Item2
0   0.417497    0.748412
1   0.896681   -0.557322
2   0.576657       NaN

Using minor_axis

可以使用方法 panel.minor_axis(index). 访问数据。

Data can be accessed using the method panel.minor_axis(index).

# creating 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)

它的 output 如下所示 −

Its output is as follows −

       Item1       Item2
0   -0.128637   -1.047032
1    0.896681   -0.557322
2    0.571668    0.431953
3   -0.144234    1.302466

Note − 观察维度中的变化。

Note − Observe the changes in the dimensions.