I'm trying to get the second to last non-null column per row, where the null could be in any column. Solutions such as this don't work due to where the null can be anywhere: Pandas select the second to last column which is also not nan
Not Ideal Solution: I was able to solve it with the following code, but there has to be a more concise way to write this. Any feedback would be appreciated.
data = [[1, 10, np.nan, np.nan], [2, 15, 13, np.nan], [9, 14, np.nan, np.nan]]
df = pd.DataFrame(data, columns = ['a', 'b', 'c', 'd'])
df['count_nulls'] = len(df.columns) - df.apply(lambda x: x.count(), axis=1)
df['count_nonnull'] = df.apply(lambda x: x.count(), axis=1)-1
df['new_index'] = np.where(df['count_nonnull']==1, 1,
np.where(df['count_nonnull']==0,0, df['count_nonnull'] - 1))
df['value'] = df.values[np.arange(len(df)), df['new_index']-1]
df