Search code examples
pythonpandaspie-chart

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


Solution

  • 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)