Getting the last non-nan index of a sorted numpy matrix or pandas dataframe

后端 未结 5 1810
野的像风
野的像风 2020-12-21 04:50

Given a numpy array (or pandas dataframe) like this:

import numpy as np

a = np.array([
[1,      1,      1,    0.5, np.nan, np.nan, np.nan],
[1,      1,              


        
5条回答
  •  情歌与酒
    2020-12-21 05:38

    Well here is a way to do it. Probably not the most efficient though:

    list(map(lambda x: [i for i, x_ in enumerate(x) if not np.isnan(x_)][-1], a))
    

    Also it will fail if any row is fully 'nan' because python will try to do getitem on an empty list.

提交回复
热议问题