Denver
Denver

Reputation: 245

groupby - python pandas dataframe

I have a dataframe with columns date,name,id (data is redundant).
Now i want to obtain the frequency of combination (date,name,id) For that am applying groupby on my dataframe:

df.groupby(['date','uname','id']).size()

which is giving me result like:

date        uname                id       size                         
2016-02-11  [email protected]            111       1
            [email protected]            1080      2
            [email protected]           5315      1
                                 5317      1
            [email protected]     536       2
2-16-02-12  [email protected]       588       1
                                 593       2    
            [email protected]       322       1

But i want my result to be like:

date        uname                id       size                         
2016-02-11  [email protected]            111       1
2016-02-11  [email protected]            1080      2
2016-02-11  [email protected]           5315      1
2016-02-11  [email protected]           5317      1
2016-02-11  [email protected]     536       2
2-16-02-12  [email protected]       588       1
2-16-02-12  [email protected]       593       2    
2-16-02-12  [email protected]       322       1

Appreciate suggestions

Upvotes: 3

Views: 278

Answers (1)

jezrael
jezrael

Reputation: 862406

You need reset_index:

df.groupby(['date','uname','id']).size().reset_index()

         date             uname    id  size
0  2016-02-11         [email protected]   111     1
1  2016-02-11         [email protected]  1080     2
2  2016-02-11        [email protected]  5315     1
3  2016-02-11        [email protected]  5317     1
4  2016-02-11  [email protected]   536     2
5  2-16-02-12    [email protected]   588     1
6  2-16-02-12    [email protected]   593     2
7  2-16-02-12    [email protected]   322     1

Upvotes: 3

Related Questions