Crosstab using pandas
Before creating the barplot we should create cross-tabulation using pandas.
Syntax: pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name=’All’, dropna=True, normalize=False
Code:
Python
# importing the pandas library import pandas as pd # Reading the csv file and storing it # in a variable df = pd.read_csv( 'Data.csv' ) # Crosstab function is called # 2 parameters are passed # The table is stored in a variable crosstb = pd.crosstab(df.Nationality, df.Handedness) |
Output:
Using pandas crosstab to create a bar plot
In this article, we will discuss how to create a bar plot by using pandas crosstab in Python. First Lets us know more about the crosstab, It is a simple cross-tabulation of two or more variables.