import numpy as np
from scipy import sparse
X = sparse.lil_matrix((100, 100))
X[1, 17] = 1
X[17, 17] = 1
indices = np.nonzero(X)
X.tocsc()[indices]
How to efficiently remove columns from a sparse matrix that only contain zeros?