Tags:

crosstab() method in Python Pandas is used to compute a simple cross tabulation of two or more factors. It computes computes a frequency table of the factors unless an array of values and an aggregation function are passed, by default.

What Parameters can be used in crosstab() method of Python Pandas

index
It should be array-like, Series, or list of arrays/Series
It is used for Values to group by in the rows.
columns
It should be array-like, Series, or list of arrays/Series
It is used for Values to group by in the columns.
values
array-like, optional
Array of values to aggregate according to the factors. Requires aggfunc be specified.
rownames
sequence, default None
If passed, must match number of row arrays passed.
colnames
sequence, default None
If passed, must match number of column arrays passed.
aggfunc
function, optional
If specified, requires values be specified as well.
margins
bool, default False
Add row/column margins (subtotals).
margins_name
str, default ‘All’
Name of the row/column that will contain the totals when margins is True.
dropna
bool, default True
To drop columns whose entries are all NaN.
normalize
bool, {‘all’, ‘index’, ‘columns’}, or {0,1}, default False
Normalize by dividing all values by the sum of values.

  • If passed ‘all’ or True, will normalize over all values.
  • If passed ‘index’ will normalize over each row.
  • If passed ‘columns’ will normalize over each column.
  • If margins is True, will also normalize margin values.