Python: How can I iterate 20 columns and find the top column?

changhons :

I am new and maybe my question is silly so I apologise in advance.

Basically I have data like this,

ID | Scope 1 | Scope 2 | Scope 3 | Scope 4 | ... | Scope 30|   
1  | True    |  True   | True    |  False  | ... |   True  |
2  | True    |  True   | True    |  False  | ... |   False |
3  | True    |  True   | True    |  False  | ... |   True  |
4  | True    |  False  | False   |  False  | ... |   False |

I would like to make a new column called Top Scope and have the highest scope number that has True as output.

I am trying for loop but I am not failing. :( Can you please help me? I would really appreciate it.

jezrael :

Use DataFrame.filter for all columns, check order of columns by DataFrame.iloc and slicing and last use DataFrame.idxmax:

df['Top Scope'] = df.filter(like='Scope').idxmax(axis=1)
#seelcting all columns without first with reversed order
#df['Top Scope'] = df.iloc[:, :0:-1].idxmax(axis=1)
print (df)
   ID  Scope 1  Scope 2  Scope 3  Scope 4  Scope 30 Top Scope
0   1     True     True     True    False      True  Scope 30
1   2     True     True     True    False     False   Scope 3
2   3     True     True     True    False      True  Scope 30
3   4     True    False    False    False     False   Scope 1

More general solution is necessary for avoid wrong output if all False values with numpy.where and DataFrame.any for test at least one True per rows:

df1 = df.filter(like='Scope').iloc[:, ::-1]
df['Top Scope'] = np.where(df1.any(axis=1), df1.idxmax(axis=1), 'no match')
print (df)
   ID  Scope 1  Scope 2  Scope 3  Scope 4  Scope 30 Top Scope
0   1     True     True     True    False      True  Scope 30
1   2     True     True     True    False     False   Scope 3
2   3     True     True     True    False      True  Scope 30
3   4     True    False    False    False     False   Scope 1
4   5    False    False    False    False     False  no match

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=379348&siteId=1