简体   繁体   English

如何从列表中删除Nan并扩展到列表列表

[英]How to remove Nan's from a list and extend to a list of lists

I am trying to extend a list to a separate list of lists in it's correct order. 我正在尝试将列表扩展到单独的列表列表,其顺序正确。 But some of the values are Nan's. 但有些价值观是Nan的。 When I do this I get an error ValueError: could not convert string to float: . 当我这样做时,我得到一个错误ValueError: could not convert string to float: . Would it be more efficient to iterate through the list removing Nan's and then extend to a list? 迭代删除Nan的列表然后扩展到列表会更有效吗?

An example of the dataset and code is shown below: 数据集和代码的示例如下所示:

Data: 数据:

X    Y
5    6
Nan  Nan
10   5
Nan  Nan
8    2

n=0
for column in data :
    if n == 0 :
        n+=1
        continue
    visuals[0].extend([float(column[0])])
    visuals[1].extend([float(column[1])])

Following on from the comments after @Mahesh Karia's code. 继@Mahesh Karia的代码之后的评论。 The dummy data works fine but my dataset returns empty lists. 虚拟数据工作正常,但我的数据集返回空列表。 An example of both is provided below: 下面提供了两者的示例:

data_1 = [['Nan', 5, 'Nan', 10, 'Nan', 8],
        ['Nan', 6, 'Nan', 5, 'Nan', 2]]

data_2 = [[nan, -5891.3764, -5901.0081390000005, -5939.977304, -5921.11797],
        [nan, 3339.025337, 3346.9211149999996, 3356.405148, 3412.836335]]

So data_1 works but data_2 returns an empty list? 所以data_1工作但data_2返回一个空列表?

def is_number(s):
    try:
        if str(s).lower() != "nan":
            float(s)
            return True
    except ValueError:
        pass
    return False


data_2 = [['nan', -5891.3764, -5901.0081390000005, -5939.977304, -5921.11797],
        ['nan', 3339.025337, 3346.9211149999996, 3356.405148, 3412.836335]]

visuals = [[],[]]

visuals[0].extend([float(_) for _ in data_2[0] if is_number(_)])
visuals[1].extend([float(_) for _ in data_2[1] if is_number(_)])

print visuals

output: 输出:

[[-5891.3764, -5901.0081390000005, -5939.977304, -5921.11797], [3339.025337, 3346.9211149999996, 3356.405148, 3412.836335]]

I would do this with a list comprehension that removes all the nan values. 我会用列表理解来删除所有的nan值。

data = ['1', '2', "nan", '4']
[float(datum) for datum in data if datum != "nan"]

Which easily extends to lists of lists. 这很容易扩展到列表列表。

data = [['1', '2', 'nan', '4'], ['5', '6', 'nan']]
[[float(bar) for bar in foo if bar != "nan"] for foo in data]

Assuming of course that there's only one string value that you need to catch. 当然假设您只需要捕获一个字符串值。 If there's multiple you'll probably want to consider either a function that does custom handling, or a try statement. 如果有多个,您可能想要考虑执行自定义处理的函数或try语句。

A more general solution would be to wrap the float conversion in a try/except block: 更通用的解决方案是将float转换包装在try / except块中:

for column in data:
    ...
    try:
        visuals[0].extend([float(column[0])])
    except ValueError:
        pass

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

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