Pyplot error "only size-1 arrays can be converted to Python scalars"

0 votes

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]])
Jan 28 in Python by Nisha
1,913 views

1 answer to this question.

0 votes

You can plot the chart by taking the total column to plot the group. Refer to the code 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()
answered Jan 28 by Omkar
• 67,460 points

Related Questions In Python

0 votes
0 answers

Python error "only size-1 arrays can be converted to Python scalars"

I'm trying to plot the exponential and ...READ MORE

Jul 9 in Python by JTM
153 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 226 views
+4 votes
6 answers
+3 votes
2 answers

how to print array integer without [] bracket in python like result = 1,2,3,4,5

Hey @abhijmr.143, you can print array integers ...READ MORE

answered Aug 4, 2018 in Python by Omkar
• 67,460 points

edited Aug 8, 2018 by Omkar 641 views
0 votes
1 answer

How to list only text files in a directory using python?

Well, you are using a complex way. ...READ MORE

answered Feb 3 in Python by Omkar
• 67,460 points
200 views