如何展平 Pandas groupby DataFrame?

How to flatten Pandas groupby DataFrame?

我有一个按日期分组的 Pandas DataFrame 和 'outcome':

api_logs.groupby([api_logs.index.date, 'Outcome']).size()
            Outcome
2017-04-22  Success      7
2017-04-24  Failure     32
            Success     59
2017-04-25  Failure     23
            Success     91
2017-04-26  Failure      1
            Success     59
2017-04-27  Failure      3
            Success      1
2017-04-28  Failure      1
            Success      2
2017-04-29  Success      3
2017-05-03  Failure     38
2017-05-04  Failure      6
            Success    727

如何将嵌套数据展平,使其结构如下?

            Failure    Success
2017-04-22                   7
2017-04-24       32         59
2017-04-25       23         91
2017-04-26        1         59
2017-04-27        3          1
2017-04-28        1          2
2017-04-29        3
2017-05-03       38
2017-05-04        6        727

我的最终目标是在图表中一起绘制失败和成功,所以也许有完全不同的方法?

使用 unstack 进行整形:

df = api_logs.groupby([api_logs.index.date, 'Outcome']).size().unstack()
print (df)
Outcome     Failure  Success
2017-04-22      NaN      7.0
2017-04-24     32.0     59.0
2017-04-25     23.0     91.0
2017-04-26      1.0     59.0
2017-04-27      3.0      1.0
2017-04-28      1.0      2.0
2017-04-29      NaN      3.0
2017-05-03     38.0      NaN
2017-05-04      6.0    727.0

也可以通过参数 fill_value:

NaNs 替换为 0
df = api_logs.groupby([api_logs.index.date, 'Outcome']).size().unstack(fill_value=0)

print (df)
Outcome     Failure  Success
2017-04-22        0        7
2017-04-24       32       59
2017-04-25       23       91
2017-04-26        1       59
2017-04-27        3        1
2017-04-28        1        2
2017-04-29        0        3
2017-05-03       38        0
2017-05-04        6      727