简体   繁体   中英

pandas pivot-table how to add nested columns

df

    billsec       disposition    Date           Hour
0   185            ANSWERED     2016-11-01       00
1   0             NO ANSWER     2016-11-01       00
2   41             ANSWERED     2016-11-01       01
3   4              ANSWERED     2016-12-02       05

There is a table, me need to get out of it a summary table with the following data:

在此输入图像描述

The rows are hours of the day, and the columns are the days, in the days of the total number of calls / missed / total duration of calls.

How to add additional columns (All, Lost, Time) in this table. I have so far turned out only to calculate the total duration of calls per hour, and their total number. Truth in different tables...

df.pivot_table(rows='Hour',cols='Date',aggfunc=len,fill_value=0)
df.pivot_table(rows='Hour',cols='Date',aggfunc=sum,fill_value=0)

IIUC you can do it this way:

assuming we have the following DataFrame:

In [248]: df
Out[248]:
             calldate  billsec disposition
0 2016-11-01 00:05:26      185    ANSWERED
1 2016-11-01 00:01:26        0   NO ANSWER
2 2016-11-01 00:05:19       41    ANSWERED
3 2016-11-01 00:16:02        4    ANSWERED
4 2016-11-02 01:16:02       55    ANSWERED
5 2016-11-02 02:02:02        2   NO ANSWER

we can do the following:

funcs = {
    'billsec': {
        'all':'size',
        'time':'sum'
    },
    'disposition': {
        'lost': lambda x: (x == 'NO ANSWER').sum()
    }
}

(df.assign(d=df.calldate.dt.strftime('%d.%m'), t=df.calldate.dt.hour)
   .groupby(['t','d'])[['billsec','disposition']].agg(funcs)
   .unstack('d', fill_value=0)
   .swaplevel(axis=1)
   .sort_index(level=[0,1], axis=1)
)

yields:

d 01.11           02.11
    all time lost   all time lost
t
0     4  230    1     0    0    0
1     0    0    0     1   55    0
2     0    0    0     1    2    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM