'Get for each row the last column name with a certain value
I have this kind of dataframe, and I'm looking to get for each row the last column name equals to 1
Here is an example of my dataframe
              col1   col2   col3    col4   col5   col6    
2020-03-31    nan    nan    nan     nan    nan    nan    
2020-04-01    nan    nan    nan     nan    1.0    1.0   
2020-04-02    nan    nan    nan     1.0    nan    nan     
2020-04-03    nan    nan    nan     nan    nan    nan   
and I'm looking to get something like this:
              col1   col2   col3    col4   col5   col6   result
2020-03-31    nan    nan    nan     nan    nan    nan    nan
2020-04-01    nan    nan    nan     nan    1.0    1.0    col6
2020-04-02    nan    nan    nan     1.0    nan    nan    col4
2020-04-03    nan    nan    nan     nan    nan    nan    nan
how can I achieve such result ?
Solution 1:[1]
df['result'] = df.apply(lambda x: x[::-1].idxmax(), axis=1)
print(df)
            col1  col2  col3  col4  col5  col6 result
2020-03-31   NaN   NaN   NaN   NaN   NaN   NaN    NaN
2020-04-01   NaN   NaN   NaN   NaN   1.0   1.0   col6
2020-04-02   NaN   NaN   NaN   1.0   NaN   NaN   col4
2020-04-03   NaN   NaN   NaN   NaN   NaN   NaN    NaN
Solution 2:[2]
df.where(df==1).agg(lambda x: x.last_valid_index(), axis=1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | jch | 
| Solution 2 | Marat | 
