Cross tab is used to compute a simple cross-tabulation of two (or more) factors. By default, it computes a frequency table of the factors unless an array of values and an aggregation function are passed.
So this recipe is a short example on What is a cross tab function and when is it used. Let's get started.
import pandas as pd
Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays.
x = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c']) y = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
Here we have two categorical dataset x and y.
Now, our dataset is ready.
Simply use crosstab function to perform the operation.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the final output.
Here 'c' and 'f' are not represented in the data and will not be shown in the output because dropna is True by default. Set dropna=False to preserve categories with no data.