Find index of the first and/or last value in a column that is not NaN
You can agg
:
df.notna().agg({'x1':'idxmax','x2':'idxmax','x3':lambda x: x[::-1].idxmax()})
#df.notna().agg({'x1':'idxmax','x2':'idxmax','x3':lambda x: x[x].last_valid_index()})
x1 503
x2 502
x3 504
Another way would be to check if first row is nan and according to that apply the condition:
np.where(df.iloc[0].isna(),df.notna().idxmax(),df.notna()[::-1].idxmax())
[503, 502, 504]
first_valid_index() and last_valid_index() can be used.
>>> df
x1 x2 x3
Depth
500 NaN NaN 25.0
501 NaN NaN 27.0
502 NaN 33.0 24.0
503 4.0 32.0 18.0
504 12.0 45.0 5.0
505 8.0 38.0 NaN
>>> df["x1"].first_valid_index()
503
>>> df["x2"].first_valid_index()
502
>>> df["x3"].first_valid_index()
500
>>> df["x3"].last_valid_index()
504
IIUC
df.stack().groupby(level=1).head(1)
Out[619]:
Depth
500 x3 25.0
502 x2 33.0
503 x1 4.0
dtype: float64