Create a Bar Chart in Python

Your goal

You need to create a bar chart for a categorical variable with associated numerical data in a Pandas DataFrame.

If you have only categorical data and you need to generate counts, you will need to create a frequency table before you can create the bar chart.

Step-by-step tutorial

Let's start by creating a simple DataFrame whose data we want to display.

>>> import pandas as pd
>>> import matplotlib.pyplot as plt
>>> df = pd.DataFrame({'holdings': [180.71, 619.32, 249.87]}, index=['AAPL', 'SBUX', 'UBER'])
>>> df
AAPL    180.71
SBUX    619.32
UBER    249.87

We can use the DataFrame object itself to create the bar chart. The categories are the DataFrame's index set, and we specify the values in the call:

>>> plt.clf()
Stock holdings (USD) in a portfolio
Stock holdings (USD) in a portfolio