Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (11.4k points)

I am trying to plot pie chart using the below code, but I am getting the error "only size-1 arrays can be converted to Python scalars". Can you help me resolve the issue?

Complete code:

import pandas as pd
import matplotlib.pyplot as plt
dir(pd)
prisoner_df=pd.read_csv("prisoners.csv")

#Syntax to display last and first 5 rows of records
print(prisoner_df.tail())
print(prisoner_df.head())

dir(prisoner_df)

# Syntax to get only non zero values of rows

new_value=prisoner_df.iloc[:,[2,3,4,5]].mean(axis=1) >0
prisoner_df[new_value]

# create new columns
prisoner_df["total_benefitted"]=prisoner_df.sum(axis=1)
series_value=pd.Series(prisoner_df.sum(axis=0))
series_value

prisoner_df.append(series_value,ignore_index=True)
prisoner_df.at[35, "STATE/UT"]="Totals"

#Plotting

#plot with each state name on the x -axis and their total benefitted
plt.pie(prisoner_df.iloc[35:,[2,3,4,5,6]])

1 Answer

+1 vote
by (32.3k points)
edited by

You can plot the chart by taking the total column to plot the group. Just follow the code given below:

import pandas as pd

import matplotlib.pyplot as plt

prisoner_df=pd.read_csv("prisoners.csv")

# Syntax to get only non zero values of rows

prisoner_df['Total']= prisoner_df.iloc[:,[2,3,4,5]].mean(axis=1)

#print(prisoner_df.head())

#Plotting

labels=[]

states=prisoner_df['STATE/UT']

total=prisoner_df['Total']

patches, texts = plt.pie(total,shadow=True,startangle=90)

#plt.legend(patches, states)

plt.axis('equal')

plt.tight_layout()

plt.show()

To know more about this you can have a look at the following video:-

Browse Categories

...