Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas pivot to sparse

from scipy.sparse import csr_matrix
from pandas.api.types import CategoricalDtype

person_c = CategoricalDtype(sorted(frame.person.unique()), ordered=True)
thing_c = CategoricalDtype(sorted(frame.thing.unique()), ordered=True)

row = frame.person.astype(person_c).cat.codes
col = frame.thing.astype(thing_c).cat.codes
sparse_matrix = csr_matrix((frame["count"], (row, col)), 
                           shape=(person_c.categories.size, thing_c.categories.size))

>>> sparse_matrix
<3x4 sparse matrix of type '<class 'numpy.int64'>'
     with 6 stored elements in Compressed Sparse Row format>

>>> sparse_matrix.todense()
matrix([[0, 1, 0, 1],
        [1, 0, 0, 1],
        [1, 0, 1, 0]], dtype=int64)


dfs = pd.SparseDataFrame(sparse_matrix, 
                         index=person_c.categories, 
                         columns=thing_c.categories, 
                         default_fill_value=0)
>>> dfs
        a   b   c   d
 him    0   1   0   1
  me    1   0   0   1
 you    1   0   1   0
Comment

PREVIOUS NEXT
Code Example
Python :: python beautifulsoup get option tag value 
Python :: if statement in python 
Python :: can serializer returns an object in django 
Python :: twitter api tutorial python 
Python :: python decorator 
Python :: data must be 1-dimensional pd.dataframe 
Python :: python curl 
Python :: python mann kendall test 
Python :: sort dict 
Python :: restart python after script execution 
Python :: python math functions 
Python :: python tkinter dynamic toggle button 
Python :: django cleanup 
Python :: pandas reset index start from 0 
Python :: fast input python 
Python :: decimal to binary python 
Python :: function in the input function python 
Python :: sort values within groups pandas dataframe 
Python :: confusion matrix with seaborn heatmap 
Python :: rename all columns 
Python :: python web framework 
Python :: stack in python using linked list 
Python :: python script that turns bluetooth on 
Python :: python list index() 
Python :: covariance in python 
Python :: dockerfile to run python script 
Python :: loop through list of lists jinja 
Python :: python logical operators 
Python :: array creation in numpy 
Python :: import matplotlib pyplot as plt 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =