📜  Python中的 pandas.crosstab()函数

📅  最后修改于: 2022-05-13 01:55:36.143000             🧑  作者: Mango

Python中的 pandas.crosstab()函数

此方法用于计算两个(或更多)因素的简单交叉表。默认情况下,除非传递值数组和聚合函数,否则计算因子的频率表。

以下是上述方法的实现以及一些示例:

示例 1:

Python3
# importing packages
import pandas
import numpy
  
# creating some data
a = numpy.array(["foo", "foo", "foo", "foo",
                 "bar", "bar", "bar", "bar",
                 "foo", "foo", "foo"],
                dtype=object)
  
b = numpy.array(["one", "one", "one", "two",
                 "one", "one", "one", "two",
                 "two", "two", "one"],
                dtype=object)
  
c = numpy.array(["dull", "dull", "shiny",
                 "dull", "dull", "shiny",
                 "shiny", "dull", "shiny",
                 "shiny", "shiny"],
                dtype=object)
  
# form the cross tab
pandas.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])


Python3
# importing package
import pandas
  
# create some data
foo = pandas.Categorical(['a', 'b'], 
                         categories=['a', 'b', 'c'])
  
bar = pandas.Categorical(['d', 'e'], 
                         categories=['d', 'e', 'f'])
  
# form crosstab with dropna=True (default)
pandas.crosstab(foo, bar)
  
# form crosstab with dropna=False
pandas.crosstab(foo, bar, dropna=False)


输出 :

示例 2:

Python3

# importing package
import pandas
  
# create some data
foo = pandas.Categorical(['a', 'b'], 
                         categories=['a', 'b', 'c'])
  
bar = pandas.Categorical(['d', 'e'], 
                         categories=['d', 'e', 'f'])
  
# form crosstab with dropna=True (default)
pandas.crosstab(foo, bar)
  
# form crosstab with dropna=False
pandas.crosstab(foo, bar, dropna=False)

输出 :