Crosstab() function

In [1]:
import numpy as np
import pandas as pd
In [2]:
p = np.array(["s1", "s1", "s1", "s1", "b1", "b1",
              "b1", "b1", "s1", "s1", "s1"], dtype=object)
In [3]:
q = np.array(["one", "one", "one", "two", "one", "one",
              "one", "two", "two", "two", "one"], dtype=object)
In [4]:
r = np.array(["x", "x", "y", "x", "x", "y",
              "y", "x", "y", "y", "y"],
             dtype=object)
In [5]:
pd.crosstab(p, [q, r], rownames=['p'], colnames=['q', 'r'])
Out[5]:
q one two
r x y x y
p
b1 1 2 1 0
s1 2 2 1 2

Here ‘r’ and ‘s’ are not represented in the data and will not be shown in the output because dropna is True by default.
Set dropna=False to preserve categories with no data.

In [6]:
s1 = pd.Categorical(['p', 'q'], categories=['p', 'q', 'r'])
In [7]:
b1 = pd.Categorical(['s', 't'], categories=['s', 't', 'u'])
In [8]:
pd.crosstab(s1, b1)
Out[8]:
col_0 s t
row_0
p 1 0
q 0 1
In [9]:
pd.crosstab(s1, b1, dropna=False)
Out[9]:
col_0 s t u
row_0
p 1 0 0
q 0 1 0
r 0 0 0