分组依据/数据透视

时间:2019-04-10 21:46:10

标签: python pandas group-by pivot

dummy_df = pd.DataFrame({
    'accnt' : [101, 102, 103, 104, 101, 102, 103, 104, 101, 102, 103, 104, 101, 102, 103, 104, 101, 102, 103, 104],
    'value' : [10, 20, 30, 40, 5, 2, 6, 48, 22, 23, 24, 25, 18, 25, 26, 14, 78, 72, 54, 6],
    'category' : [1,1,1,1,2,2,2,2,1,1,2,2,3,3,3,3,1,3,2,3]
})

dummy_df

accnt   value   category
 101     10      1
 102     20      1
 103     30      1
 104     40      1
 101     5       2
 102     2       2
 103     6       2
 104     48      2
 101     22      1
 102     23      1
 103     24      2
 104     25      2
 101     18      3
 102     25      3
 103     26      3
 104     14      3
 101     78      1
 102     72      3
 103     54      2
 104     6       3

我想获得如下数据框:

accnt   sum_val_c1  count_c1    sum_val_ct2 count_c2    sum_val_c3  count_c3
  101       110        3           5           1              18     1
  102       43         2           2           1              97     2
  103       30         1           84          3              26     1
  104       40         1           73          2              20     2

这是将类别的出现汇总为count_c#,并将该类别的值汇总为sum_val_c#,然后按accnt进行分组。我曾尝试使用pivot()groupby(),但我知道我缺少一些东西。

2 个答案:

答案 0 :(得分:5)

使用window.onload = function(){ const Http = new XMLHttpRequest; const url = "http://localhost:5001/"; Http.open("GET",url,true); Http.withCredentials = true; Http.setRequestHeader('Content-type','text/plain'); Http.send(); } server side: app.get("/",stuff); function stuff(req,res) { res.cookie("ss","ss"); console.log("stuff"); res.header("Access-Control-Allow-Origin", "http://127.0.0.1:5500"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Credentials", "true"); res.send(); } groupbyagg

unstack

类似地,使用u = df.groupby(['accnt', 'category'])['value'].agg(['sum', 'count']).unstack(1) u.columns = u.columns.map('{0[0]}_c{0[1]}'.format) u sum_c1 sum_c2 sum_c3 count_c1 count_c2 count_c3 accnt 101 110 5 18 3 1 1 102 43 2 97 2 1 2 103 30 84 26 1 3 1 104 40 73 20 1 2 2

pivot_table

答案 1 :(得分:1)

熊猫有做到这一点的方法。

pivot2 = dummy_df.pivot_table(values='value', index='accnt', columns='category', aggfunc=['count', 'sum'])

这将返回如下数据框:

         count        sum
category     1  2  3    1   2   3
accnt
101          3  1  1  110   5  18
102          2  1  2   43   2  97
103          1  3  1   30  84  26
104          1  2  2   40  73  20