跳过genfromtxt中缺少值的行 [英] Skip Rows with missing values in genfromtxt

查看:197
本文介绍了跳过genfromtxt中缺少值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何加载csv.文件至少在单元格为空时会跳过行吗? 我的csv文件很大(超过1000行和14个列):

how can i load a csv. file into an array skipping rows when at least on cell is empty? my csv file is large (over 1000 rows and 14 colums):

1;4;3
;1;3
;;6
3;4;7

我想跳过写第2行和第3行,因为它们缺少值(x; 1; 3)(x; x; 6) 其他所有已完成的行都应写入数组...

i want to skip writing row 2 and 3 cause they have missing values (x;1;3) (x;x;6) all the other rows that are complete should be written to an array...

这些行(每行中都包含完整"信息)应写入矩阵(数组)

These rows (with "full" information in each row should be written to a matrix (array)

M = np.genfromtxt(file.csv, delimiter=";",dtype=float)

推荐答案

在所有行中进行读取可能会更容易,然后仅保留其中的行而不会丢失数据.

It'll probably be easier to read in all the rows and then keep only the ones without missing data.

>>> M = np.genfromtxt("miss.csv", delimiter=";", dtype=float)
>>> M
array([[  1.,   4.,   3.],
       [ nan,   1.,   3.],
       [ nan,  nan,   6.],
       [  3.,   4.,   7.]])
>>> M = M[~np.isnan(M).any(axis=1)]
>>> M
array([[ 1.,  4.,  3.],
       [ 3.,  4.,  7.]])

(这假设您不会在miss.csv中保留要保存的nan作为值.如果这样做,这会有些棘手.)

(This assumes that you won't have nan as a value in miss.csv which you want to preserve. If you do, it'd be a little trickier.)

这篇关于跳过genfromtxt中缺少值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆