Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to python, i need to plot a bar chart from the output which is in percentages

I used the below code to filter out the top 5 Items by percentages in the DataFrame

df = df['Country'].value_counts(normalize=True).mul(100).round(2).astype(str) +'%' 
df.head(5)

Output

United States     32.78%
India              8.79%
France             4.75%
United Kingdom     4.35%
Germany            3.96%

Name: Country, dtype: object

How can I plot the output using a bar graph?

like image 677
Zac Avatar asked Dec 10 '25 20:12

Zac


2 Answers

You need to get your percentages back to a numeric datatype:

df.str[:-1].astype(float).plot.bar()

Output:

enter image description here

like image 149
Scott Boston Avatar answered Dec 13 '25 11:12

Scott Boston


import pandas as pd
df = pd.read_clipboard(header=None, sep=r"[ ]{2,}", names=['country', 'percent'])
df['percent'] = df['percent'].astype(str).str.strip('%')
df['percent'] = pd.to_numeric(df['percent'])
df.set_index('country', inplace=True)
df.plot(kind='bar')

enter image description here

like image 38
Matthew Borish Avatar answered Dec 13 '25 11:12

Matthew Borish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!