目录

上一个主题

5.4. Pandas数据帧(DataFrame)

下一个主题

5.6. Pandas基本功能


>>> from env_helper import info; info()
页面更新时间: 2020-03-08 17:32:59
操作系统/OS: Linux-4.19.0-8-amd64-x86_64-with-debian-10.3 ;Python: 3.7.3

5.5. Pandas面板(Panel)

面板(Panel)是3D容器的数据。面板数据一词来源于计量经济学,部分源于名称:Pandas - pan(el)-da(ta)-s。

3轴(axis)这个名称旨在给出描述涉及面板数据的操作的一些语义。它们是 -

  • items - axis 0,每个项目对应于内部包含的数据帧(DataFrame)。

  • major_axis - axis 1,它是每个数据帧(DataFrame)的索引(行)。

  • minor_axis - axis 2,它是每个数据帧(DataFrame)的列。

pandas.Panel()

可以使用以下构造函数创建面板 -

pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)

构造函数的参数如下 -

参数

描述

data

数据采取各种形式,如:ndarray,series,map,lists,dict,constant和另一个数据帧(DataFrame)

items

axis=0

major_axis

axis=1

minor_axis

axis=2

dtype

每列的数据类型

copy

复制数据,默认 - false

创建面板

可以使用多种方式创建面板 -

  • 从ndarrays创建

  • 从DataFrames的dict创建

从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)
<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
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)
注意 - 观察空面板和上面板的尺寸大小,所有对象都不同。

从DataFrame对象的dict创建面板

>>> #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)
<class 'pandas.core.panel.Panel'>
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
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)

创建一个空面板

可以使用Panel的构造函数创建一个空面板,如下所示:

>>> #creating an empty panel
>>> import pandas as pd
>>> p = pd.Panel()
>>> print(p)
<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
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)

执行上面示例代码,得到以下结果 -

从面板中选择数据

要从面板中选择数据,可以使用以下方式 -

Items
Major_axis
Minor_axis

使用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'])
          0         1         2
0  1.365258 -0.414416  0.771233
1  0.301667 -2.215284 -0.670355
2 -1.242691 -0.274139  0.038543
3 -1.898036  0.126907 -2.583758
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)

上面示例有两个数据项,这里只检索item1。结果是具有4行和3列的数据帧(DataFrame),它们是Major_axis和Minor_axis维。

使用major_axis

可以使用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))
      Item1     Item2
0  1.393896  0.021365
1 -2.304740  0.249750
2 -1.337894       NaN
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)

使用minor_axis

可以使用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))
      Item1     Item2
0  0.916560 -0.891744
1 -1.073971 -2.125420
2 -1.056071 -0.961596
3 -1.737044  1.215809
/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py:2878: FutureWarning:
Panel is deprecated and will be removed in a future version.
The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method
Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.
Pandas provides a .to_xarray() method to help automate this conversion.

  exec(code_obj, self.user_global_ns, self.user_ns)
注意 - 观察尺寸大不的变化。