Search code examples
pythondataframerowspie-chart

Can I make a pie chart based on indexes in Python?


Could you please help me if you know how to make a pie chart in Python from it?

This is a reproducible example how the df looks like. However, I have way more rows over there.

import pandas as pd
data = [["70%"], ["20%"], ["10%"]]
example = pd.DataFrame(data, columns = ['percentage'])
example.index = ['Lasiogl', 'Centella', 'Osmia']
example

Solution

  • You can use matplotlib to plot the pie chart using dataframe and its indexes as labels of the chart:

    import matplotlib.pyplot as plt
    import pandas as pd
    data = ['percentage':["70%"], ["20%"], ["10%"]]
    example = pd.DataFrame(data, columns = ['percentage'])
    my_labels = 'Lasiogl', 'Centella', 'Osmia'
    plt.pie(example,labels=my_labels,autopct='%1.1f%%')
    plt.show()