Skip Rows with missing values in genfromtxt

為{幸葍}努か 提交于 2019-12-24 01:38:48

问题


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

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)

回答1:


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.]])

(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.)



来源:https://stackoverflow.com/questions/20007017/skip-rows-with-missing-values-in-genfromtxt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!