如何使用熊猫分组和求和

时间:2019-06-13 16:06:19

标签: pandas dataframe group-by

这是我的数据框:

    RefactoringType      Detail
0   Move Method         com.onegravity.colorpicker.ColorPickerDialog
1   Move Source Folder  NaN
2   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
3   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
4   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
5   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
6   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
7   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
8   Move Attribute      com.onegravity.colorpicker.ColorPickerDialog
9   Rename Method       com.onegravity.colorpicker.ColorPickerDialog

Detail列包含类的名称。我需要计算每个类中RefactoringType的数量。 我尝试过:

g1 = df1.groupby(["Detail", "RefactoringType"]).sum()

它为我回报:

Detail                                         RefactoringType
com.onegravity.colorpicker.ColorPickerDialog    Move Attribute
                                                Move Method
                                                Rename Method

我需要这样的结果:

 Class                                       Move Method   Move Attribute Rename Method

com.onegravity.colorpicker.ColorPickerDialog  1             1                    1  

PS:在我将定义所有重构类型之后。 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试:

df.groupby(['Detail', 'RefactoringType']).size()\
    .unstack().fillna(0, downcast='infer')

对于以下测试DataFrame:

df = pd.DataFrame(data=[
    [ 'Move Attribute', 'ColorPickerDialog' ],
    [ 'Move Method',    'ColorPickerDialog' ],
    [ 'Rename Method',  'ColorPickerDialog' ],
    [ 'Rename Method',  'ColorPickerDialog' ],
    [ 'Rename Method',  'XxxxxxxxxxxDialog' ],
    [ 'Rename Method',  'XxxxxxxxxxxDialog' ]],
    columns=['RefactoringType', 'Detail'])

我知道了

RefactoringType    Move Attribute  Move Method  Rename Method
Detail                                                       
ColorPickerDialog               1            1              2
XxxxxxxxxxxDialog               0            0              2