Pandas数据透视表格式

2024-09-28 03:12:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用单个aggfunc格式化多个值的pivot_table输出。多个值应该并排出现

数据帧为:

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

我希望输出为:

^{pr2}$

使用pivot_表


Tags: 数据namedatetablecallpivotkpibts
2条回答

这就是你要找的吗?注意:为了便于打印,我将列重命名为“call_v”和“call_d”。在

    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

基本上是堆积后的物质的聚集。在

您可以使用groupby或pivot_table函数聚合call_volume和call_drop。在

Python代码:

# 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})

输出:

^{pr2}$

编辑

下面是将kpi_date作为列的代码

# 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

相关问题 更多 >

    热门问题