Search code examples
python-3.xpandaspie-chart

Python: Plot pie chart for every groups in Pandas


Below is the data frame retrieved after grouping my data set:

df1 = data.groupby(['gender','Segment']).agg(Total_Claim = ('claim_amount', 'sum'))
df1['Total_Claim']=df1['Total_Claim'].astype(int)
df1

The output of the same is:

                    Total_Claim
gender  Segment 
Female  Gold        2110094
        Platinum    2369761
        Silver      1897617
Male    Gold        2699208
        Platinum    2096489
        Silver      2347217

What would be the most efficient way of plotting a pie chart between the aggregated value of claim amount based on gender and segment?


Solution

  • You can get Series and ploting by Series.plot.pie and autopct parameter for percentages:

    df['Total_Claim'].plot.pie(autopct='%1.1f%%')
    

    g