新彩天欢迎您!
幻海优品

Python Pandas pandas.crosstab函数方法的使用

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, 可选的

要根据因子聚合的值数组,要求指定aggfunc

rownamessequence, 默认为 None

如果传递,则必须匹配传递的行数组数量。

colnamessequence, 默认为 None

如果传递,则必须匹配传递的列数组数量。

aggfuncfunction, 可选的

如果指定,也需要指定值。

marginsbool, 默认为 False

添加行/列边距(小计)。

margins_namestr, 默认为 ‘All’

当margin为True时,将包含总数的行/列的名称。

dropnabool, 默认为 True

不要包含条目都是NaN的列。

normalizebool, {‘all’, ‘index’, ‘columns’}, 或 {0,1}, 默认为 False

用所有值除以值的和进行归一化。

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        twoc   dull shiny dull shinyabar    1     2    1     0foo    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  erow_0a      1  0b      0  1>>> pd.crosstab(foo, bar, dropna=False)col_0  d  e  frow_0a      1  0  0b      0  1  0c      0  0  0

免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除