AneesBaqir
AneesBaqir

Reputation: 453

How to create a pie-chart from pandas DataFrame?

I have a dataframe, with Count arranged in decending order, that looks something like this:

df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})

But with more than 50 rows.

I would like to create a pie chart for the top 10 topics and rest of them to be summed up and represent its percentange as label "Others" in the pie chart. Is it possible to exclude the pie labels against each pie, and mention them seperately in a legend?

Thanking in anticipation

Upvotes: 1

Views: 1734

Answers (3)

jezrael
jezrael

Reputation: 862481

Replace Topic by Other if no top N in Series.where and then aggregate sum with Series.plot.pie:

N = 10
df['Topic'] = df['Topic'].where(df['Count'].isin(df['Count'].nlargest(N)), 'Other')

s = df.groupby('Topic')['Count'].sum()

pie = df.plot.pie(y='Count', legend=False)

#https://stackoverflow.com/a/44076433/2901002
labels = [f'{l}, {s:0.1f}%' for l, s in zip(s.index, s / s.sum())]
plt.legend(bbox_to_anchor=(0.85, 1), loc='upper left', labels=labels)

Upvotes: 1

mozway
mozway

Reputation: 260335

You need to craft a new dataframe. Assuming your counts are sorted in descending order (if not, use df.sort_values(by='Count', inplace=True)):

TOP = 10
df2 = df.iloc[:TOP]
df2 = df2.append({'Topic': 'Other', 'Count': df['Count'].iloc[TOP:].sum()},
                 ignore_index=True)

df2.set_index('Topic').plot.pie(y='Count', legend=False)

Example (N=10, N=5):

pie chart pie chart N=5

Percentages in the legend:

N = 5
df2 = df.iloc[:N]
df2 = df2.append({'Topic': 'Other', 'Count': df['Count'].iloc[N:].sum()}, ignore_index=True)

df2.set_index('Topic').plot.pie(y='Count', legend=False)
leg = plt.legend(labels=df2['Count'])

output:

plot with legend

Upvotes: 1

Dinesh Jinjala
Dinesh Jinjala

Reputation: 29

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'Topic': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'],
                   'Count': [80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20]})
df.index = df.Topic
plot = df.plot.pie(y='Count', figsize=(5, 5))
plt.show()

Use documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.pie.html

Upvotes: 0

Related Questions