下面是我的输入数据框
df = pd.DataFrame({'Level_DB': ['Level 1 Experienced' ,'Level 2 Expert', 'Level 1 Experienced', 'Level 2 Expert', 'Level 3 Thought Leader', 'Level 1 Experienced', 'Non-Certified', 'Level 3 Thought Leader', 'Certified', 'Certified', np.nan, 'Level 1 Experienced'],
'Level_Legacy' :[ 'Certified', 'Level 1 Experienced', 'Level 3 Thought Leader', 'Level 3 Thought Leader Recert', 'Level 3 Thought Leader Recert', 'Non-Certified', 'non-certified', 'Level 2 Expert Recert', 'Level 1 Experienced', 'Non-Certified', 'Certified', '']})
然后,在比较两个输入列“ Level_DB”和“ Level_Legacy”并选择最高优先级值之后,应生成目标列:“输出”。优先级列表如下
priority_List = ['Level 3 Thought Leader', 'Level 3 Thought Leader New', 'Level 3 Thought Leader Recert', 'Level 3 Thought Leader Recert Lapsed',
'Level 2 Expert', 'Level 2 Expert New', 'Level 2 Expert Recert', 'Level 2 Expert Recert Lapsed',
'Level 1 Experienced', 'Level 1 Experienced New', 'Level 1 Experienced Recert', 'Level 1 Experienced Recert Lapsed', 'Certified', 'Non-Certified' , 'non-certified']
期望的最终DataFrame具有所需的“输出”列,如下所示:
一开始我什么都没打。请帮助
答案 0 :(得分:1)
创建的想法ordered categoricals,其重整形状为DataFrame.stack
,因此输出是每个max
的{{1}}值:
level=0
答案 1 :(得分:1)
我们可以在Series.map
处enumerating
处使用priority_list
,以获得最低的索引,该索引的顺序最高:
dct_priority = {j:i for i, j in enumerate(priority_List)}
dct_priority_reverse = {i:j for i, j in enumerate(priority_List)}
df['Output'] = df.apply(lambda x: x.map(dct_priority)).min(axis=1).map(dct_priority_reverse)
Level_DB Level_Legacy Output
0 Level 1 Experienced Certified Level 1 Experienced
1 Level 2 Expert Level 1 Experienced Level 2 Expert
2 Level 1 Experienced Level 3 Thought Leader Level 3 Thought Leader
3 Level 2 Expert Level 3 Thought Leader Recert Level 3 Thought Leader Recert
4 Level 3 Thought Leader Level 3 Thought Leader Recert Level 3 Thought Leader
5 Level 1 Experienced Non-Certified Level 1 Experienced
6 Non-Certified non-certified Non-Certified
7 Level 3 Thought Leader Level 2 Expert Recert Level 3 Thought Leader
8 Certified Level 1 Experienced Level 1 Experienced
9 Certified Non-Certified Certified
10 NaN Certified Certified
11 Level 1 Experienced Level 1 Experienced