How can I “sparsify” on two values?
问题 consider the the pandas series s n = 1000 s = pd.Series([0] * n + [1] * n, dtype=int) s.memory_usage() 8080 I can "sparsify" this by using to_sparse s.to_sparse(fill_value=0).memory_usage() 4080 But I only have 2 types of integers. I'd think I could sparsify twice. Is there a way to do this? 回答1: Since you tagged this with scipy , I'll show you what a scipy.sparse matrix is like: In [31]: n=100 In [32]: arr=np.array([[0]*n+[1]*n],int) In [33]: M=sparse.csr_matrix(arr) In [34]: M.data Out[34]: