如果有任何可能使此代码更简单,我将不胜感激!我试图摆脱零的行。第一列是日期。如果所有其他列都为零,则必须删除它们。列数不同。
import numpy as np
condition = [ np.any( list(x)[1:] ) for x in r]
r = np.extract( condition, r )
如果有任何可能使此代码更简单,我将不胜感激!我试图摆脱零的行。第一列是日期。如果所有其他列都为零,则必须删除它们。列数不同。
import numpy as np
condition = [ np.any( list(x)[1:] ) for x in r]
r = np.extract( condition, r )
You can avoid the list comprehension and instead use fancy indexing:
#!/usr/bin/env python
import numpy as np
import datetime
r=np.array([(datetime.date(2000,1,1),0,1),
(datetime.date(2000,1,1),1,1),
(datetime.date(2000,1,1),1,0),
(datetime.date(2000,1,1),0,0),
])
r=r[r[:,1:].any(axis=1)]
print(r)
# [[2000-01-01 0 1]
# [2000-01-01 1 1]
# [2000-01-01 1 0]
if r is an ndarray, then r[:,1:] is a view with the first column removed. r[:,1:].any(axis=1) is a boolean array, which you can then use as a "fancy index"