Issue
I have a 2D numpy array that looks like
a = np.array(
[
[1,2,np.nan,np.nan],
[1,33,45,np.nan],
[11,22,3,78],
]
)
I need to extract the last non null value per row i.e. [2, 45, 78]
Please guide on how to get it.
thanks
Solution
Break this into two sub-problems.
- Remove
nan
s from each row - Select last element from the array
[r[~np.isnan(r)][-1] for r in a]
produces
[2.0, 45.0, 78.0]
Answered By - pavi2410
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.