pandas pivot to sparse

Solutions on MaxInterview for pandas pivot to sparse by the best coders in the world

showing results for - "pandas pivot to sparse"
Melina
10 Oct 2019
1from scipy.sparse import csr_matrix
2from pandas.api.types import CategoricalDtype
3
4person_c = CategoricalDtype(sorted(frame.person.unique()), ordered=True)
5thing_c = CategoricalDtype(sorted(frame.thing.unique()), ordered=True)
6
7row = frame.person.astype(person_c).cat.codes
8col = frame.thing.astype(thing_c).cat.codes
9sparse_matrix = csr_matrix((frame["count"], (row, col)), \
10                           shape=(person_c.categories.size, thing_c.categories.size))
11
12>>> sparse_matrix
13<3x4 sparse matrix of type '<class 'numpy.int64'>'
14     with 6 stored elements in Compressed Sparse Row format>
15
16>>> sparse_matrix.todense()
17matrix([[0, 1, 0, 1],
18        [1, 0, 0, 1],
19        [1, 0, 1, 0]], dtype=int64)
20
21
22dfs = pd.SparseDataFrame(sparse_matrix, \
23                         index=person_c.categories, \
24                         columns=thing_c.categories, \
25                         default_fill_value=0)
26>>> dfs
27        a   b   c   d
28 him    0   1   0   1
29  me    1   0   0   1
30 you    1   0   1   0
31