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

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

Trying to show Count by department in Python

问题

以下是已翻译的部分:

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

  1. 部门 任务 状态
  2. 销售 销售 待处理
  3. 销售 演示 完成
  4. 技术 合并数据 完成
  5. 技术 合并数据 待处理
  6. 技术 演示 完成

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

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

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

参考代码:

  1. 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:

  1. Department Task Status
  2. Sales Sell Pendiing
  3. Sales Presentation Complete
  4. Tech Merge Data Complete
  5. Tech Consolidate Pending
  6. 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:

  1. Department Status Count
  2. Sales Completed 1
  3. 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:

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

答案1

得分: 3

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

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

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

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

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

  1. df2[df2['Status'] == 'Complete']

输出结果:

  1. Department Status Count
  2. 0 Sales Complete 1
  3. 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:

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

Output (for your sample data):

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

You can then filter that on Status if required:

  1. df2[df2['Status'] == 'Complete']

Output:

  1. Department Status Count
  2. 0 Sales Complete 1
  3. 2 Tech Complete 2

答案2

得分: 1

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

  1. df = df[df['状态'] == '完成'].groupby('部门')['任务'].count().reset_index()
  2. df.columns = ['部门', '计数']
  3. df['状态'] = '已完成'
  4. df = df.reindex(columns=['部门', '状态', '计数'])
  5. print(df)
英文:

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

  1. df = df[df['Status'] == 'Complete'].groupby('Department')['Task'].count().reset_index()
  2. df.columns = ['Department', 'Count']
  3. df['Status'] = 'Completed'
  4. df = df.reindex(columns=['Department', 'Status', 'Count'])
  5. print(df)

  1. Department Status Count
  2. 0 Sales Completed 1
  3. 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:

确定