02 Aug

Crosstab Table

The panda's crosstab function is really useful for creating when you want to create pivot tables for dimensional features. with one line of code, your can plot two dimensions against each other fairly quickly. The function has some more advanced multi-indexing functionality but I haven't looked into it very much.

Import Preliminaries

In [3]:
# Import modules
import pandas as pd

# Create a dataframe
df = pd.DataFrame(data=[['Red','Red','Red','Blue','Green'],
                        ['Honda','Acura','Honda','Nissan','Nissan']])
df = df.T
df.columns = ['Color','Model']

# View the datafram
df
Out[3]:
Color Model
0 Red Honda
1 Red Acura
2 Red Honda
3 Blue Nissan
4 Green Nissan
In [4]:
# Create a pivot table
pd.crosstab(df['Color'],df['Model'])
Out[4]:
Model Acura Honda Nissan
Color
Blue 0 0 1
Green 0 0 1
Red 1 2 0

Author: Kavi Sekhok