Python is an elegant way to select the highest weighted value in a set

I have a list such that

l = ['xyz','abc','mnq','qpr']

These values ​​are weighted so that xyz>abc>mnq>qpr I have a pandas dataframe with a column that has sets of values.

                      COL_NAME    
0         set(['xyz', 'abc'])     
1         set(['xyz']) 
2         set(['mnq','qpr']) 

Now I want to select the highest values ​​in the sets, so that after applying the user-defined function, I stay with

                  COL_NAME    
0         set(['xyz'])     
1         set(['xyz']) 
2         set(['mnq']) 

Is there an elegant way to do this without resorting to the weights dictionary?

+4
source share
1 answer

you can use pd.Categoricalwith parameter ordered=Trueand set categories=l[::-1]to get the order you need.

def max_cat(x):
    return set([pd.Categorical(x, l[::-1], True).max()])

df.COL_NAME.apply(max_cat)

0    {xyz}
1    {xyz}
2    {mnq}
Name: COL_NAME, dtype: object
+3
source

Source: https://habr.com/ru/post/1662176/


All Articles