如何在Pandas数据框中按列值分组

时间:2018-11-12 10:18:49

标签: python pandas pandas-groupby data-science

我有这样的pandas数据框。我想要按App_Name在单独的变量中分组

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

我想按App_Name对数据进行分组,并为每个App_Name存储在单独的列表或数据框中,如下所示:

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

1 个答案:

答案 0 :(得分:3)

groupby对象转换为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

编辑:

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