问题
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