且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

跳过genfromtxt中缺少值的行

更新时间:2023-02-05 19:30:22

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

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