Prioritize based on Column Value and select row

前端 未结 1 848
余生分开走
余生分开走 2021-01-20 18:22

I want to give priority on Multiple columns and select row based on priority

I wanted to select ID\'s with RC priority in Category Column and Pending Priority in Sta

1条回答
  •  有刺的猬
    2021-01-20 18:38

    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
    

    0 讨论(0)
提交回复
热议问题