本文介绍了根据列值确定优先级并选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为多列"赋予优先级,并根据优先级选择行
I want to give priority on Multiple columns and select row based on priority
我想在类别"列中选择具有RC优先级的ID,在状态"列中选择待处理优先级",并相应地选择行"
I wanted to select ID's with RC priority in Category Column and Pending Priority in Status column and select Rows accordingly
示例:输入数据框
ID Category Status Date
1 GC Pending 01-03-2015
1 RC Resolved 05-10-2016
1 GC Resolved 06-03-2017
2 RC Pending 09-08-2016
2 RC Resolved 10-05-2014
3 GC Resolved 10-08-2018
3 RC Pending 13-05-2019
4 GC Pending 10-06-2018
4 GC Resolved 15-09-2014
输出数据框
ID Category Status Date
1 RC Resolved 05-10-2016
2 RC Pending 09-08-2016
3 RC Pending 13-05-2019
4 GC Pending 10-06-2018
推荐答案
通过将列表传递给categories
参数,然后按 DataFrame.sort_values
,最后使用 DataFrame.drop_duplicates
:
Convert columns to ordered categoricals with set priority by passing list to categories
parameter, then sorting by 3 columns by DataFrame.sort_values
and last remove duplicates with DataFrame.drop_duplicates
:
df['Category'] = pd.Categorical(df['Category'], ordered=True, categories=['GC','RC'])
df['Status'] = pd.Categorical(df['Status'], ordered=True, categories=['Resolved','Pending'])
df = df.sort_values(['ID','Category','Status']).drop_duplicates('ID', keep='last')
print (df)
ID Category Status Date
1 1 RC Resolved 05-10-2016
3 2 RC Pending 09-08-2016
6 3 RC Pending 13-05-2019
7 4 GC Pending 10-06-2018
这篇关于根据列值确定优先级并选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!