具有标称数据的Pandas Panel的重采样功能

时间:2014-08-26 12:50:04

标签: python pandas group-by panel resampling

我使用PanelPandas创建了Python构造(请参阅代码)。之后,我用resample()函数推广(总和)时间。

import pandas as pd
import numpy as np

time_rng = pd.date_range('1/1/2000', '31/1/2000', freq='D')
PanelData = pd.Panel(np.random.randn(3, 31, 6),
                 items=['Fish', 'Meat', 'Vegetables'],
                 major_axis=time_rng,
                 minor_axis=['a', 'b', 'c', 'd', 'e', 'f'])

数据请求:

PanelData
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 31 (major_axis) x 6 (minor_axis)
Items axis: Fish to Vegetables
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-31 00:00:00
Minor_axis axis: a to f

PanelData.resample('W', how='sum', axis=1)
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 6 (major_axis) x 6 (minor_axis)
Items axis: Fish to Vegetables
Major_axis axis: 2000-01-02 00:00:00 to 2000-02-06 00:00:00
Minor_axis axis: a to f

如何使用给定列表(a =&gt;区域1,b =&gt;区域1,b =&gt;区域2等)推广Minor_axis,PanelData将如下所示:< / p>

<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 31 (major_axis) x 6 (minor_axis)
Items axis: Fish to Vegetables
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-31 00:00:00
Minor_axis axis: Zone 1 to Zone 3

zones = ['Zone 1', 'Zone 1', 'Zone 2', 'Zone 3', 'Zone 1', 'Zone 2']

注意:我正在使用Python v.2.7.6IPython Notebook v.2.1.0

1 个答案:

答案 0 :(得分:1)

对我的讯问感到抱歉。这是groupby()函数的一个问题。我找到了我要求的解决方案。

zones = ['Zone 1', 'Zone 1', 'Zone 2', 'Zone 3', 'Zone 1', 'Zone 2']

PanelData.groupby(zones, axis=2).sum()
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 31 (major_axis) x 3 (minor_axis)
Items axis: Fish to Vegetables
Major_axis axis: 2000-01-01 00:00:00 to 2000-01-31 00:00:00
Minor_axis axis: Zone 1 to Zone 3