Python Pandas中的复杂Groupby或Pivot表计算

时间:2016-05-16 00:10:10

标签: python python-2.7 pandas group-by pivot-table

我的数据框分组如下:

UNIT CA    DATE     SCP      TIME       LABEL       VALUES1      VALUES2           
R001 A058  08-01-13 01-00-00 01:00:00  REGULAR    340751.000   194975.000
                             05:00:00  REGULAR    340753.000   194975.000
                             09:00:00  REGULAR    341251.000   194984.000
                             09:39:56  REGULAR    341440.000   194994.000
                             13:00:00  REGULAR    341808.000   195061.000
                             17:00:00  REGULAR    342030.000   195295.000
                             21:00:00  REGULAR    342214.000   195659.000
                    01-00-01 01:00:00  REGULAR    245262.000   221709.000
                             05:00:00  REGULAR    245262.000   221709.000
                             09:00:00  REGULAR    245428.000   221742.000
                             09:39:56  REGULAR    245508.000   221754.000
                             13:00:00  REGULAR    245620.000   221856.000
                             17:00:00  REGULAR    245679.000   222178.000
                             21:00:00  REGULAR    245743.000   222604.000

我想为每个SCP提取VALUE1和VALUE2的最大值和最小值,计算差值,并按以下格式返回:

UNIT CA    DATE      SCP     DIFF OF MAX - MIN VALUE1   DIFF OF MAX - MIN VALUE2         
R001 A058  08-01-13 01-00-00        ....                         ....
                    01-00-01        ....                         ....

我无法弄清楚该怎么做。我相信必须有一些方法可以使用groupby或pivot_table来完成它。

提前致谢。

1 个答案:

答案 0 :(得分:2)

IIUC,.groupby() level应该有效。从您的示例数据开始:

df.set_index(['UNIT', 'CA', 'DATE', 'SCP'], inplace=True)

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 14 entries, (R001, A058, 2013-08-01 00:00:00, 01-00-00) to (R001, A058, 2013-08-01 00:00:00, 01-00-01)
Data columns (total 4 columns):
TIME       14 non-null object
LABEL      14 non-null object
VALUES1    14 non-null int64
VALUES2    14 non-null int64
dtypes: int64(2), object(2)

                                   TIME    LABEL  VALUES1  VALUES2
UNIT CA   DATE       SCP                                          
R001 A058 2013-08-01 01-00-00  01:00:00  REGULAR   340751   194975
                     01-00-00  05:00:00  REGULAR   340753   194975
                     01-00-00  09:00:00  REGULAR   341251   194984
                     01-00-00  09:39:56  REGULAR   341440   194994
                     01-00-00  13:00:00  REGULAR   341808   195061
                     01-00-00  17:00:00  REGULAR   342030   195295
                     01-00-00  21:00:00  REGULAR   342214   195659
                     01-00-01  01:00:00  REGULAR   245262   221709
                     01-00-01  05:00:00  REGULAR   245262   221709
                     01-00-01  09:00:00  REGULAR   245428   221742
                     01-00-01  09:39:56  REGULAR   245508   221754
                     01-00-01  13:00:00  REGULAR   245620   221856
                     01-00-01  17:00:00  REGULAR   245679   222178
                     01-00-01  21:00:00  REGULAR   245743   222604

MultiIndex级别上进行分组,并对两列中的每一列应用max()min()的差异:

df.groupby(level=['UNIT', 'CA', 'DATE', 'SCP'])['VALUES1', 'VALUES2'].apply(lambda x: x.max()-x.min())

                               VALUES1  VALUES2
UNIT CA   DATE       SCP                       
R001 A058 2013-08-01 01-00-00     1463      684
                     01-00-01      481      895