user8044236
user8044236

Reputation:

How to build sparse matrix based on pandas table?

I have pandas DataFrame where each row is of the following format:

 i   j   d
----------
10  24 0.6

How to efficiently build and fill sparse matrix (from scipy.sparse) by values using i and j as row and column indices and d as value?

The sparse matrix is square with known number of rows and columns K. All the numbers in columns i and j are less than K.

Upvotes: 0

Views: 412

Answers (1)

llllllllll
llllllllll

Reputation: 16404

Use this:

coo_matrix((df.d.values, (df.i.values, df.j.values)), shape=(K,K))

Upvotes: 1

Related Questions