how to create a pie chart from csv file using python

13,099

Solution 1

This is very simple.

import pandas as pd
from matplotlib.pyplot import pie, axis, show
%matplotlib inline

df = pd.read_csv ('chart_work.csv')

sums = df.groupby(df["Product Name;"])["Number Of Bugs"].sum()
axis('equal');
pie(sums, labels=sums.index);
show()

enter image description here

Solution 2

The pie chart does not 'know' that you want all items with same product name grouped and summed over in your chart. so you have to do that first:

df = df.groupby(["Product Name;"]).sum()

This sets the product name column as index of the df so change your product_data column selection to this:

product_data = df.index   
Share:
13,099
Ganesha
Author by

Ganesha

Passionate about web developing(learning in progress)

Updated on June 05, 2022

Comments

  • Ganesha
    Ganesha almost 2 years

    I have this CSV data file, I'm trying to make a pie chart using this data

    I'm a beginner in python and don't understand how to create a pie chart using the three columns, please help!

    working solution code would be more helpful!

    My code:

    import pandas as pd
    import matplotlib.pyplot as plt 
    
    df = pd.read_csv ('chart_work.csv')
    
    product_data = df["Product Name;"]   
    bug_data = df["Number Of Bugs"]                      
    colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]    
    
    plt.pie(bug_data , labels=product_data , colors=colors,
    autopct='%1.1f%%', shadow=True, startangle=140)
    
    plt.show()
    

    the pie chart which is outputed by this code is distorted, any help?

    Chart I'm getting:

    enter image description here