Create a Pie Chart in Python

Your goal

You need to create a pie 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 pie 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 pie chart. The categories are the DataFrame's index set, and we specify the values in the call:

>>> clf()
>>> df.plot.pie(y="holdings")
Stock holdings (USD) in a portfolio
Stock holdings (USD) in a portfolio