pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False) [源代码]
计算两个(或多个)因子的简单交叉表。
默认情况下,计算因子的频率表,除非传递值数组和聚合函数。
| 参数: | index:array-like, Series, 或 arrays/Series的list 要在行中进行分组的值。 xpath:array-like, Series, 或 arrays/Series的list 列中要分组的值。 values:array-like, 可选的 要根据因子聚合的值数组,要求指定 rownames: 如果传递,则必须匹配传递的行数组数量。 colnames: 如果传递,则必须匹配传递的列数组数量。 aggfunc: 如果指定,也需要指定值。 margins: 添加行/列边距(小计)。 margins_name: 当margin为True时,将包含总数的行/列的名称。 dropna: 不要包含条目都是NaN的列。 normalize: 用所有值除以值的和进行归一化。 1)如果传入' all '或True,将对所有值进行规范化。 2)如果传递' index ',将对每一行进行规范化。 3)如果传递' columns ',将对每一列进行规范化。 4)如果margin为True,也将使margin值规范化。 | 
| 返回: | DataFrame 交叉表格的数据。 | 
Notes
任何传递的Series都将使用它们的name属性,除非为交叉表指定行名或列名。
传递的任何包含Categorical数据的输入都将使其所有类别包含在交叉表中,即使实际数据不包含任何特定类别的实例。
如果没有重叠的索引,则返回一个空的DataFrame。
例如,
>>> a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
...               "bar", "bar", "foo", "foo", "foo"], dtype=object)
>>> b = np.array(["one", "one", "one", "two", "one", "one",
...               "one", "two", "two", "two", "one"], dtype=object)
>>> c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny",
...               "shiny", "dull", "shiny", "shiny", "shiny"],
...              dtype=object)
>>> pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])
b   one        two
c   dull shiny dull shiny
a
bar    1     2    1     0
foo    2     2    1     2这里' c '和' f '没有在数据中表示,也不会显示在输出中,因为dropna默认为True。设置dropna=False保存没有数据的类别。
>>> foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
>>> bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
>>> pd.crosstab(foo, bar)
col_0  d  e
row_0
a      1  0
b      0  1
>>> pd.crosstab(foo, bar, dropna=False)
col_0  d  e  f
row_0
a      1  0  0
b      0  1  0
c      0  0  0