hepzi
hepzi

Reputation: 445

How to Group by column value in Pandas Data frame

I have pandas dataframe like this. I want group by App_Name in seperate variable

App_Name    Date        Response    Gross Revenue
com.apple.tiles2    2018-10-13  3748.723574 24133394
com.orange.thescore 2018-10-13  2034.611964 8273607
com.number.studio   2018-10-13  1807.756545 33736740
com.orange.thescore 2018-10-14  4671.930435 38575556
com.number.studio   2018-10-14  3533.461547 38726087
com.banana.com      2018-10-14  2920.33747  86230313
com.apple.tiles2    2018-10-15  3986.434851 35928884
com.number.studio   2018-10-15  2044.759823 76526368
com.apple.tiles2    2018-10-16  2610.214035 30611434
com.alpha.studio    2018-10-16  1731.429858 11643154
com.banana.com      2018-10-16  1601.387403 13781285
com.alpha.studio    2018-10-17  2769.373388 13198984
com.banana.com      2018-10-17  2205.359489 21974901
com.orange.thescore 2018-10-17  1820.852862 7565015
com.alpha.studio    2018-10-18  2784.822039 24217875
com.banana.com      2018-10-18  2545.899329 28361412
com.orange.thescore 2018-10-18  2052.207745 7544861

I want to group data by App_Name and stored in sepearte list or dataframe for each App_Name, something like this given below:

App_Name    Date        Response    Gross Revenue
com.alpha.studio    2018-10-16  1731.429858 11643154
com.alpha.studio    2018-10-17  2769.373388 13198984
com.alpha.studio    2018-10-18  2784.822039 24217875

App_Name    Date        Response    Gross Revenue
com.apple.tiles2    2018-10-13  3748.723574 24133394
com.apple.tiles2    2018-10-15  3986.434851 35928884
com.apple.tiles2    2018-10-16  2610.214035 30611434

App_Name    Date        Response    Gross Revenue
com.banana.com      2018-10-14  2920.33747  86230313
com.banana.com      2018-10-16  1601.387403 13781285
com.banana.com      2018-10-17  2205.359489 21974901
com.banana.com      2018-10-18  2545.899329 28361412

App_Name    Date        Response    Gross Revenue
com.number.studio   2018-10-14  3533.461547 38726087
com.number.studio   2018-10-13  1807.756545 33736740
com.number.studio   2018-10-15  2044.759823 76526368

App_Name    Date        Response    Gross Revenue
com.orange.thescore 2018-10-13  2034.611964 8273607
com.orange.thescore 2018-10-14  4671.930435 38575556
com.orange.thescore 2018-10-17  1820.852862 7565015
com.orange.thescore 2018-10-18  2052.207745 7544861

Upvotes: 1

Views: 1199

Answers (1)

jezrael
jezrael

Reputation: 862751

Convert groupby object to dictionary of DataFrames:

d = dict(tuple(df.groupby('App_Name')))

print (d['com.alpha.studio'])
            App_Name        Date     Response     Gross  Revenue
9   com.alpha.studio  2018-10-16  1731.429858  11643154      NaN
11  com.alpha.studio  2018-10-17  2769.373388  13198984      NaN
14  com.alpha.studio  2018-10-18  2784.822039  24217875      NaN

EDIT:

d1 = {}
for k, v in d.items():
     d1[k] = v['Gross Revenue'].rolling(2).mean()

Upvotes: 3

Related Questions