简体   繁体   中英

Remove elements from list of lists based on condition

I have a list of lists (from a certain column of a dataframe where each element of col is a list) which I transformed to an array of shape (2819, 11). 在此处输入图像描述

I am trying to find code that does not include for loops, to eliminate lists that include np.nan

You can use np.isnan() combined withany() to index only the rows you want:

l = np.array([
    [1, 2, 3],
    [1, np.nan, 4],
    [5, 6, 7],
    [np.nan, np.nan, 7]
])


l[~(np.isnan(l).any(axis=1))]

#array([[1., 2., 3.],
#       [5., 6., 7.]])

This uses ~ to invert the boolean values from isnan() so you get the rows where it is false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM