pandas.crosstab
pandas 3.1.1 · General functions · function
Count combinations of categories in a table. Supply values and aggfunc to calculate another summary.
Call syntax
pd.crosstab(…) Full signature
pd.crosstab(
index,
columns,
values=None,
rownames=None,
colnames=None,
aggfunc=None,
margins=False,
margins_name='All',
dropna=True,
normalize=False,
)Run the example
Edit the example and select Run Python to see the result.
Parameters
| Parameter | Requirement | Accepted values and behavior |
|---|---|---|
index |
Required Positional or keyword |
array-like, Series, or list of arrays/Series Supply row labels or control how existing row labels are used. |
columns |
Required Positional or keyword |
array-like, Series, or list of arrays/Series Supply column labels or control how columns are handled. |
values |
Optional Positional or keyword Default None |
array-like Supply values to transform, match, aggregate, or use as data. |
rownames |
Optional Positional or keyword Default None |
sequence |
colnames |
Optional Positional or keyword Default None |
sequence |
aggfunc |
Optional Positional or keyword Default None |
function Choose an aggregation name, callable, list, or mapping as supported. |
margins |
Optional Positional or keyword Default False |
bool Choose whether summary totals are added for rows and columns. |
margins_name |
Optional Positional or keyword Default 'All' |
str Set the label for summary totals. |
dropna |
Optional Positional or keyword Default True |
bool Choose whether to drop missing values or missing keys from consideration. |
normalize |
Optional Positional or keyword Default False |
bool, {'all', 'index', 'columns'}, or {0,1} Request normalized output, such as proportions for counts or midnight for datetimes. |
Returns
DataFrame