简体   繁体   中英

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  a@abc.com            111       1
            b@abc.com            1080      2
            ar@ata.com           5315      1
                                 5317      1
            aru628@gmail.com     536       2
2-16-02-12  ch45@gmail.com       588       1
                                 593       2    
            doy5@gmail.com       322       1

But i want my result to be like:

date        uname                id       size                         
2016-02-11  a@abc.com            111       1
2016-02-11  b@abc.com            1080      2
2016-02-11  ar@ata.com           5315      1
2016-02-11  ar@ata.com           5317      1
2016-02-11  aru628@gmail.com     536       2
2-16-02-12  ch45@gmail.com       588       1
2-16-02-12  ch45@gmail.com       593       2    
2-16-02-12  doy5@gmail.com       322       1

Appreciate suggestions

You need reset_index :

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

         date             uname    id  size
0  2016-02-11         a@abc.com   111     1
1  2016-02-11         b@abc.com  1080     2
2  2016-02-11        ar@ata.com  5315     1
3  2016-02-11        ar@ata.com  5317     1
4  2016-02-11  aru628@gmail.com   536     2
5  2-16-02-12    ch45@gmail.com   588     1
6  2-16-02-12    ch45@gmail.com   593     2
7  2-16-02-12    doy5@gmail.com   322     1

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