简体   繁体   English

根据条件从列表列表中删除元素

[英]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).我有一个列表列表(来自 dataframe 的某个列,其中 col 的每个元素都是一个列表),我将其转换为形状数组(2819、11)。 在此处输入图像描述

I am trying to find code that does not include for loops, to eliminate lists that include np.nan我正在尝试查找不包含 for 循环的代码,以消除包含 np.nan 的列表

You can use np.isnan() combined withany() to index only the rows you want:您可以将np.isnan()any() ) 结合使用来仅索引您想要的行:

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.这使用~来反转isnan()中的 boolean 值,因此您可以获得错误的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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