如何对一列进行分组,并根据经过修饰的值对另一列进行进一步分组并计数(Pandas Python)

时间:2019-10-30 11:14:44

标签: python pandas pandas-groupby

以下提供了所需数据表

我想先按用户名对列进行分组,并计算该用户名的单个处理代码的出现次数

User Name Disposition Code
person1    Solved
person2    Solved
person1    Solved
person2    Not Solved
person3    Solved

所需结果:

User Name Total Solved Total Not Solved
person1     2              0
person2     1              1
peson3      1              0

2 个答案:

答案 0 :(得分:2)

您想要pd.crosstab

pd.crosstab(index=df['UserName'], columns=df['DispositionCode'])

Disposition Code  Not Solved  Solved
UserName                          
person1                  0       2
person2                  1       1
person3                  0       1

答案 1 :(得分:-2)

您还可以将SqlConnectiongroupby + value_counts结合使用:

unstack

要获得join_df

选项1

new_df=df.groupby('User_Name')['Disposition_Code'].apply(lambda x: x.value_counts()).unstack(fill_value=0)
print(new_df)

           Not_Solved  Solved
User_Name                    
person1             0       2
person2             1       1
person3             0       1

选项2

join_df=df.copy()
join_df['count']=new_df.lookup(df['User_Name'],df['Disposition_Code'])
print(join_df)

  User_Name Disposition_Code  count
0   person1           Solved      2
1   person2           Solved      1
2   person1           Solved      2
3   person2       Not_Solved      1
4   person3           Solved      1