【问题标题】:Filtering data doing a lookup with comma separate values过滤数据并使用逗号分隔值进行查找
【发布时间】:2021-01-11 23:12:15
【问题描述】:

我有以下数据框,代表员工编号、他们所在的部门以及他们在公司中的角色代码,可以是“1”或“2”。在“部门名称”列上,您可以让员工担任其角色的部门(命名约定为“XX:部门名称”,其中 XX 是国家代码),或者在某些情况下,它显示为一组部门,由逗号“,”表示员工在这些部门中的角色。它看起来像这样:

  Department Name                Employee Number      Role Code   
0  AU:Dept1                         1000                     1
1  AU:Dept1, AU:Dept3               1000                     2
2  AU:Dept7                         1000                     1
3  CZ:Dept3                         1001                     2
4  CZ:Dept4, CZ:Dept6, CZ:Dept7     1001                     2
5  CZ:Dept4                         1001                     1 
6  PL:Dept1                         1002                     2
7  PL:Dept2, PL:Dept1               1002                     1
8  PL:Dept3                         1002                     2
9  SG:Dept1                         1003                     1
10 SG:Dept1                         1003                     2
11 SG:Dept2                         1003                     2

员工在每个唯一的部门名称中只能有角色 1 或角色 2,因此我需要创建一个代码来返回所有冲突的行,其中员工似乎在同一部门中同时拥有角色 1 和角色 2。那将是输出:

  Department Name                Employee Number      Role Code   
0  AU:Dept1                         1000                     1
1  AU:Dept1, AU:Dept3               1000                     2
4  CZ:Dept4, CZ:Dept6, CZ:Dept7     1001                     2
5  CZ:Dept4                         1001                     1 
6  PL:Dept1                         1002                     2
7  PL:Dept2, PL:Dept1               1002                     1
9  SG:Dept1                         1003                     1
10 SG:Dept1                         1003                     2

执行此过滤器的最佳方法是什么?

【问题讨论】:

  • 你试过了吗?邮政编码。我们可以帮助您解决代码中的特定问题。
  • 对不起,我正在度假,已经接受了答案,谢谢!

标签: python pandas lambda filter slice


【解决方案1】:

你可以这样做

df['both_role'] = df.groupby('Employee Number')['Role Code'].isin([1]).astype(int) * df.groupby('Employee Number')['Role Code'].isin([2]).astype(int) 

df[df.both_role == 1]

您可以使用员工编号进行分组,并检查每个用户的角色代码是否包含 1 和 2。如果它同时包含 1 和 2,那么您可以过滤数据框。

【讨论】:

    【解决方案2】:

    让我们尝试拆分部门名称,然后将groupby 拆分为['Employee', 'Name'],以找出哪些员工在nunique 中有两个角色:

    (df.assign(Name=df['Department Name'].str.split(', '))
       .explode('Name')
       .loc[lambda x:x.groupby(['Employee Number','Name'])
                     ['Role Code'].transform('nunique') ==2 ]
       .drop('Name', axis=1)
    )
    

    输出:

                     Department Name  Employee Number  Role Code
    0                       AU:Dept1             1000          1
    1             AU:Dept1, AU:Dept3             1000          2
    4   CZ:Dept4, CZ:Dept6, CZ:Dept7             1001          2
    5                       CZ:Dept4             1001          1
    6                       PL:Dept1             1002          2
    7             PL:Dept2, PL:Dept1             1002          1
    9                       SG:Dept1             1003          1
    10                      SG:Dept1             1003          2
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-14
      • 2022-01-25
      相关资源
      最近更新 更多