如何制作具有多个聚合的自定义分组数据框

Tim*_*ess 4 python pandas

我有一个标准数据框,如下所示:

       Id Type  Speed Efficiency Durability
0   Id001    A     OK         OK      nonOK
1   Id002    A  nonOK         OK      nonOK
2   Id003    B  nonOK      nonOK      nonOK
3   Id004    B  nonOK      nonOK         OK
4   Id005    A  nonOK      nonOK         OK
5   Id006    A     OK         OK         OK
6   Id007    A     OK      nonOK         OK
7   Id008    B  nonOK      nonOK         OK
8   Id009    C     OK         OK         OK
9   Id010    B     OK         OK      nonOK
10  Id011    C     OK      nonOK         OK
11  Id012    C     OK      nonOK         OK
12  Id013    C  nonOK         OK         OK
13  Id014    C  nonOK      nonOK         OK
14  Id015    C  nonOK      nonOK         OK
Run Code Online (Sandbox Code Playgroud)

我正在尝试获得这种输出:

  Type   Test  Speed  Efficiency  Durability
0    A     OK      3           3           3
1    A  nonOK      2           2           2
2    B     OK      1           1           2
3    B  nonOK      3           3           2
4    C     OK      3           2           6
5    C  nonOK      3           4           0
Run Code Online (Sandbox Code Playgroud)

我尝试过,df.groupby('Type').agg('count')但它没有给出预期的输出。

请问可以用 pandas 进行这种转换吗?

Ano*_*n R 5

pandas您还可以使用方法链来使用以下解决方案:

import pandas as pd

(pd.melt(df, id_vars='Type', value_vars=['Speed', 'Efficiency', 'Durability'], value_name='Test')
 .groupby(['Type', 'Test', 'variable'])
 .size()
 .reset_index()
 .pivot(index=['Type', 'Test'], columns='variable', values=0)
 .reset_index())

variable Type   Test  Durability  Efficiency  Speed
0           A     OK         3.0         3.0    3.0
1           A  nonOK         2.0         2.0    2.0
2           B     OK         2.0         1.0    1.0
3           B  nonOK         2.0         3.0    3.0
4           C     OK         6.0         2.0    3.0
5           C  nonOK         NaN         4.0    3.0
Run Code Online (Sandbox Code Playgroud)