How can I create a pie chart from a Pandas DataFrame? 2

How can I create a pie chart from a Pandas DataFrame?

Creating a pie chart from a Pandas DataFrame is a simple and effective way to visually represent data. To do so, first import the Pandas library and load your desired DataFrame. Then, select the specific column or series that you want to visualize and use the “plot.pie()” function. This will automatically generate a pie chart with the data from your DataFrame. You can further customize the chart by adding a title, labels, and adjusting the size and colors. This method is useful for quickly analyzing categorical data and identifying patterns and proportions.

Create Pie Chart from Pandas DataFrame


You can use the following basic syntax to create a pie chart from a pandas DataFrame:

df.groupby(['group_column']).sum().plot(kind='pie', y='value_column')

The following examples show how to use this syntax in practice.

Example 1: Create Basic Pie Chart

Suppose we have the following two pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'B', 'C', 'C'],
                   'points': [25, 12, 25, 14, 19, 53, 25, 29]})

#view DataFrame
print(df)

  team  points
0    A      25
1    A      12
2    B      25
3    B      14
4    B      19
5    B      53
6    C      25
7    C      29

We can use the following syntax to create a pie chart that displays the portion of total points scored by each team:

df.groupby(['team']).sum().plot(kind='pie', y='points')

Example 2: Create Custom Pie Chart

We can use the following arguments to customize the appearance of the pie chart:

  • autopct: Display percentages in pie chart
  • colors: Specify colors to use in pie chart
  • title: Add title to pie chart

The following code shows how to use these arguments in practice:

df.groupby(['team']).sum().plot(kind='pie', y='points', autopct='%1.0f%%',
                                colors = ['red', 'pink', 'steelblue'],
                                title='Points Scored by Team'))

Note that the colors will be assigned to the categories as they appear in the DataFrame.

For example, team ‘A’ appears first in the DataFrame, which is why it received the color ‘red’ in the pie chart.

The following tutorials explain how to create other common plots using a pandas DataFrame:

Cite this article

stats writer (2024). How can I create a pie chart from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-from-a-pandas-dataframe/

stats writer. "How can I create a pie chart from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-from-a-pandas-dataframe/.

stats writer. "How can I create a pie chart from a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-from-a-pandas-dataframe/.

stats writer (2024) 'How can I create a pie chart from a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-pie-chart-from-a-pandas-dataframe/.

[1] stats writer, "How can I create a pie chart from a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I create a pie chart from a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top