I’m removing some columns from a numpy array using a boolean array. Is it possible to do something similar with a list?
#Set a numpy array of booleans to True if column datatype is "Categorical"
cols_to_remove = np.array([datatype == "Categorical" for datatype in datatypes])
#Make a new array without the "Categorical" columns
cat_data = data[:, -cols_to_remove] # data here is a 2D numpy array
#Trying to do the same for a list - this way doesn't work
cat_datatypes = datatypes[-cols_to_remove] # datatypes here is a 1D list
This can be done with a list comprehension:
It
cols_to_removeis an array of indices, the following solution can be use:Here, for efficiency reasons it may be a good idea to turn
cols_to_removeinto aset.