Skip to content

Instantly share code, notes, and snippets.

@seanjtaylor
Created February 11, 2015 01:46
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save seanjtaylor/568141f04a16d518be24 to your computer and use it in GitHub Desktop.
Save seanjtaylor/568141f04a16d518be24 to your computer and use it in GitHub Desktop.
Reshaping a Pandas dataframe into a sparse matrix
import pandas as pd
import scipy.sparse as sps
df = pd.DataFrame({'tag1': ['sean', 'udi', 'bogdan'], 'tag2': ['sean', 'udi', 'udi'], 'freq': [1,2,3]})
# tag1 -> rows, tag2 -> columns
df.set_index(['tag1', 'tag2'], inplace=True)
mat = sps.coo_matrix((df.freq, (df.index.labels[0], df.index.labels[1])))
print(mat.todense())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment