MasterGG
MasterGG

Reputation: 11

Python how to plot a frequency pie chart with one column using plotly.express

I have a data frame and I only want to plot the frequency of one column, for example, the count of cars of different brands. Cars: [FORD, FORD, BMW, GMC, GMC, GMC, GMC.....] I want to plot them in pie charts of any suitable graphs, without using matplotlib.

I tried to create pivot tables, which are like Ford: 4, Chevy: 3, BMW: 5, GMC: 10. but I don't know how to access the column labels and I can't use them in plotly.

Upvotes: 1

Views: 3457

Answers (1)

Sanidhya Singh
Sanidhya Singh

Reputation: 521

You can groupby cars and get the counts in a new column count like so

df = df.groupby(['cars'])['cars'].count().reset_index(name='count')

Then you may use Plotly to render a pie chart like so

import plotly.express as px
fig = px.pie(df, values='count', names='cars', title='Cars')
fig.show()

Have some pie!

Upvotes: 3

Related Questions