简体   繁体   English

根据字典中的条件过滤长格式 Pandas DF

[英]Filtering long format Pandas DF based on conditions from the dictionary

Imagine I have an order for specialists in some coding languages with multiple criterion in JSON format:想象一下,我有一个使用 JSON 格式的多个标准的编码语言专家的订单:

request = {'languages_required': {'Python': 4,
                                  'Java': 2},
           'other_requests': []
          }

languages_required means that the candidate must have a skill in the language and the number is the minimum level of this language. language_required 表示候选人必须具备该语言的技能,并且数字是该语言的最低水平。

The format of candidates dataframe is long:候选数据框的格式很长:

df = pd.DataFrame({'candidate': ['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'd'],
                  'language': ['Python', 'Java', 'Scala', 'Python', 'R', 'Python', 'Java', 'Python', 'Scala', 'Java'],
                  'skill': [5, 4, 4, 6, 8, 1, 3, 5, 2, 2]})

That gives:这给出了:


    candidate   language    skill
0       a       Python      5
1       a       Java        4
2       a       Scala       4
3       b       Python      6
4       b       R           8
5       c       Python      1
6       c       Java        3
7       d       Python      5
8       d       Scala       2
9       d       Java        2

What I need to do is to keep the candidates and their skills in required languages that meet the requirements from the request, that is:我需要做的是使候选人及其技能保持在满足请求要求的所需语言中,即:

  1. Have skills in both mentioned languages具备上述两种语言的技能
  2. Skills in these languages are equal or higher than values in the dictionary这些语言的技能等于或高于字典中的值

So the desired output would be:所以所需的输出将是:


    candidate   language    skill
0       a       Python      5
1       a       Java        4
7       d       Python      5
9       d       Java        2

I am able to filter the candidates with the languages based on keys() of the dictionary:我可以根据字典的 keys() 使用语言过滤候选人:

lang_mask = df[df['language'].isin(request['languages_required'].keys())]\
                                                                         .groupby('candidate')['language']\
                                                                         .apply(lambda x: set(request['languages_required']).issubset(x))

...but struggle with adding the 'is higher than' per language condition. ...但是要为每种语言条件添加“高于”。 I would really appreciate some help.我真的很感激一些帮助。

You need call first condition in one step and then second in another step:您需要在一个步骤中调用第一个条件,然后在另一个步骤中调用第二个条件:

df = df[df['language'].map(request['languages_required']).le(df['skill'])]
df = df[df.groupby('candidate')['language'].transform(lambda x: set(request['languages_required']).issubset(x))]
print (df)
  candidate language  skill
0         a   Python      5
1         a     Java      4
7         d   Python      5
9         d     Java      2

Or one row solution:或一排解决方案:

df = (df[df['language'].map(request['languages_required']).le(df['skill'])]
      .pipe(lambda x: x[x.groupby('candidate')['language'].transform(lambda x: set(request['languages_required']).issubset(x))]))

print (df)
  candidate language  skill
0         a   Python      5
1         a     Java      4
7         d   Python      5
9         d     Java      2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM