简体   繁体   中英

More than one condition meet numpy select

I hv the following dataframe:

     A   B  C  D   E   F
    100  0  0  0  100  0
     0  100 0  0   0  100
   -100  0  0  0  100  0 

and this code:

cond = [
    (df['A'] == 100),
    (df['A'] == -100),
    (df['B'] == 100),
    (df['C'] == 100),
    (df['D'] == 100),
    (df['E'] == 100),
    (df['F'] == 100),
    ]
choices = ['A','neg_A', 'B', 'C','D', 'E', 'F']
df['result'] = np.select(cond, choices)

For both rows there will be two results but I want only one to be selected. I want the selection to be made with this criteria:

   +A = 67%
   -A = 68%
    B = 70%
    C = 75%
    D = 66%
    E = 54%
    F = 98%

Percentage shows accuracy rate so i would want the one with highest percentage to be preferred over the other.

Intended result:

    A   B  C  D   E   F   result
    100  0  0  0  100  0     A
     0  100 0  0   0  100    F
   -100  0  0  0  100  0    neg_A 

Little help will be appreciated. THANKS!

EDIT:

Some of the columns (like A) may have a mix of 100 and -100. Positive 100 will yield a simple A (see row 1) but a -100 should yield some other name like "neg_A" in the result (see row 3).

Let's sort the columns of dataframe based on the priority values then use .eq + .idxmax on axis=1 to get the column name with first occurrence of 100 :

# define a dict with col names and priority values
d = {'A': .67, 'B': .70, 'C': .75, 'D': .66, 'E': .54, 'F': .98}

df['result'] = df[sorted(d, key=lambda x: -d[x])].eq(100).idxmax(axis=1)

     A    B  C  D    E    F result
0  100    0  0  0  100    0      A
1    0  100  0  0    0  100      F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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