简体   繁体   中英

Pandas Pivot_table formatting

How to format the pivot_table output of multiple values with single aggfunc. multiple values should come as side by side

Data frame is:

kpi_date    ssaname              bts_name call_volume call_drop
0  2015-09-01  Bangalore   1002_NUc_Marathalli        8962      0.62
1  2015-09-03  Bangalore   1002_NUc_Marathalli        6567      1.19
2  2015-09-02  Bangalore   1002_NUc_Marathalli        7033      0.63
3  2015-09-01  Bangalore  1003_IU2_Munnekolalu        4659      1.17
4  2015-09-02  Bangalore  1003_IU2_Munnekolalu        6671      0.46

I want the output as:

2015-09-01                  2015-09-02
bts_name,   call_volume   call_drop   call_volume call_drop 

using pivot_table

You can use either groupby or the pivot_table function to aggregate call_volume and call_drop.

Python Code:

# Method 1: Using pivot_table
pd.pivot_table(df,index=["kpi_date","bts_name"],aggfunc=np.average)

# Method 2: Using groupby
df.groupby(["kpi_date", "bts_name"]).agg({"call_volume": np.average, "call_drop": np.average})

Output:

kpi_date bts_name                   call_drop    call_volume             
9/1/2015 1002_NUc_Marathalli        0.62         8962
         1003_IU2_Munnekolalu       1.17         4659
9/2/2015 1002_NUc_Marathalli        0.63         7033
         1003_IU2_Munnekolalu       0.46         6671
9/3/2015 1002_NUc_Marathalli        1.19         6567

Edit

Here is the code to get kpi_date as columns

# Python code
df.pivot_table(['call_volume', 'call_drop'], ['bts_name'], 'kpi_date')

                         call_volume                   call_drop                  
kpi_date                9/1/2015 9/2/2015 9/3/2015  9/1/2015 9/2/2015 9/3/2015
bts_name                                                                      
1002_NUc_Marathalli         8962     7033     6567      0.62     0.63     1.19
1003_IU2_Munnekolalu        4659     6671      NaN      1.17     0.46      NaN

Is this what you are looking for? Note: I renamed your columns to 'call_v' and 'call_d' for easier printing.

    kpi_date    ssaname              bts_name  call_v  call_d
0  2015-09-01  Bangalore   1002_NUc_Marathalli    8962    0.62
1  2015-09-03  Bangalore   1002_NUc_Marathalli    6567    1.19
2  2015-09-02  Bangalore   1002_NUc_Marathalli    7033    0.63
3  2015-09-01  Bangalore  1003_IU2_Munnekolalu    4659    1.17
4  2015-09-02  Bangalore  1003_IU2_Munnekolalu    6671    0.46

df.groupby(['bts_name','kpi_date']).mean().stack().unstack(level=1).unstack(level=1)

kpi_date             2015-09-01        2015-09-02        2015-09-03       
                        call_v call_d     call_v call_d     call_v call_d
bts_name                                                                  
1002_NUc_Marathalli        8962   0.62       7033   0.63       6567   1.19
1003_IU2_Munnekolalu       4659   1.17       6671   0.46        NaN    NaN

Basically it is a matter of stacking and unstacking after the aggregation.

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