Skip to content
Advertisement

for every row find the last column with value 1 in binary data frame

consider a data frame of binary numbers:

JavaScript

how do I find, for each row, the rightmost column in which a 1 is observed?

so for the dataframe above it would be:

JavaScript

Advertisement

Answer

One option is to reverse the column order, then use idxmax:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement