尝试在Python中按部门显示计数。

huangapple go评论113阅读模式
英文:

Trying to show Count by department in Python

问题

以下是已翻译的部分:

这是一段简单的代码,目前我无法想到原因。我试图按部门按状态获取任务的数量。例如:

 部门        任务             状态
 销售        销售             待处理
 销售        演示             完成
 技术        合并数据         完成
 技术        合并数据         待处理
 技术        演示             完成

我想要的是能够按部门分组统计状态列中已完成的数量。类似这样:

 部门        状态          数量
 销售        已完成         1
 技术        已完成         2

到目前为止,我的代码可以看到所有部门的计数,但我无法找出最佳的分组方式。

参考代码:

counts = df['Department'].groupby('Status').count()
英文:

This is a simple piece of code for some reason I can't think of at the moment. I am trying to get the count of task by status by department. For instance something like this:

 Department      Task           Status
   Sales         Sell           Pendiing
   Sales         Presentation   Complete
   Tech          Merge Data     Complete 
   Tech          Consolidate    Pending 
   Tech          Presentation   Complete

What I want for here is to be able to break down by department of the count of completed in the status column. Something like this:

Department    Status        Count
  Sales       Completed       1
  Tech        Completed       2

So far my code sees the count for all departments but I can't figure out the best way to group by.

Code for reference:

counts = df['Department'].groupby('Status').count()

答案1

得分: 3

你需要使用groupby方法对DepartmentStatus进行分组;然后可以对每个分组使用count方法,并根据需要对Task列进行rename。然后,如果需要,可以使用reset_index方法将数据框重置为行索引:

df2 = df.groupby(['Department', 'Status']).count().rename(columns={'Task':'Count'}).reset_index()

输出结果(对于你的示例数据):

  Department    Status Count
0      Sales  Complete     1
1      Sales  Pendiing     1
2       Tech  Complete     2
3       Tech   Pending     1

然后,如果需要,可以根据Status进行筛选:

df2[df2['Status'] == 'Complete']

输出结果:

  Department    Status  Count
0      Sales  Complete      1
2       Tech  Complete      2
英文:

You need to groupby Department and Status; you can then count each group and rename the Task column if desired. Then you can use reset_index if required to return a row-indexed dataframe:

df2 = df.groupby(['Department', 'Status']).count().rename(columns={'Task':'Count'}).reset_index()

Output (for your sample data):

  Department    Status Count
0      Sales  Complete     1
1      Sales  Pendiing     1
2       Tech  Complete     2
3       Tech   Pending     1

You can then filter that on Status if required:

df2[df2['Status'] == 'Complete']

Output:

  Department    Status  Count
0      Sales  Complete      1
2       Tech  Complete      2

答案2

得分: 1

这是一个可产生预期输出的替代方案,使用 pandas.GroupBy.count

df = df[df['状态'] == '完成'].groupby('部门')['任务'].count().reset_index()
df.columns = ['部门', '计数']

df['状态'] = '已完成'
df = df.reindex(columns=['部门', '状态', '计数'])

print(df)
英文:

Here is an alternative which will produce the expected output using pandas.GroupBy.count

df = df[df['Status'] == 'Complete'].groupby('Department')['Task'].count().reset_index()
df.columns = ['Department', 'Count']

df['Status'] = 'Completed'
df = df.reindex(columns=['Department', 'Status', 'Count'])

print(df)

  Department     Status  Count
0      Sales  Completed      1
1       Tech  Completed      2

huangapple
  • 本文由 发表于 2023年2月27日 07:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定