
2. Sidetable
The second trick involves a third party package called Sidetable which was created by Chris Moffitt.
We first need to install it.
$ python -m pip install -U sidetable #from terminal!pip install sidetable #jupyter notebook
We can now import and start using it. Once imported, it can be used as an accessor on dataframes like str and dt accessors.
import sidetable
Sidetable has 4 different functions one of which is the freq function. It returns three pieces of information about a given column:
- The number of observations (i.e. rows) for each value in the column (value_counts()).
- The percentage of each value in the entire column (value_counts(normalize=True)).
- The cumulative versions of the two above.
Let’s apply it on the age column.
import sidetabledf.stb.freq(['Age'])
The freq function counts the number of rows by default. If we pass another column using the value parameter, it will return the sum of values in that column.
df.stb.freq(['Age'], value = 'AmountSpent')
Sidetable provides three more functions. Here is a more detailed article about sidetable if you’d like read further.