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

后端 未结 5 1809
野的像风
野的像风 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:46

    If all nan values have been sorted to the end of each row, you can do something like this:

    (~np.isnan(a)).sum(axis = 1) - 1
    # array([3, 2, 6, 3, 0, 3])
    

提交回复
热议问题