Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flatten Pandas groupby DataFrame?

I have a Pandas DataFrame that is grouped by date and '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

How can I flatten the nested data, so that it is structured as below?

            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

My end-goal is to plot the failures and successes together in a chart, so perhaps there is a different approach altogether?

like image 400
Brylie Christopher Oxley Avatar asked Dec 10 '22 10:12

Brylie Christopher Oxley


1 Answers

Use unstack for reshape:

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

Also is possible replace NaNs to 0 by parameter fill_value:

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
like image 107
jezrael Avatar answered Dec 20 '22 12:12

jezrael