python - numpy.extract and numpy.any functions, is it possible to make it simpler way? -
If there is a possibility to simplify this code, then I really appreciate it! The first column is the date I am trying to get rid of rows with zero. If all other columns are zero, then they have to be deleted. The number of columns is different
You can avoid understanding the list And instead you can 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 a ndarray, then r [:, 1:] one view has been removed with the first column R [:, 1:]. Any (axis = 1) is a boolean array, which you can then use as a "fancy index"
Comments
Post a Comment