Convert Pandas dataframe to Sparse Numpy Matrix directly

numpy, pandas, python, scipy

Solution

`df.values` is a numpy array, and accessing values that way is always faster than `np.array`.

scipy.sparse.csr_matrix(df.values)

You might need to take the transpose first, like `df.values.T`. In DataFrames, the columns are axis 0.

Problem

I am creating a matrix from a Pandas dataframe as follows: ``` dense_matrix = np.array(df.as_matrix(columns = None), dtype=bool).astype(np.int) ``` And then into a sparse matrix with: ``` sparse_matrix = scipy.sparse.csr_matrix(dense_matrix) ``` Is there any way to go from a df straight to a sparse matrix? Thanks in advance.

Original source