Week12- Stacked Bar plot: TypeError: only size-1 arrays can be converted to Python scalars

I was trying to plot stacked bar plot is similar manner shown in course. Plot total and maharashtra as stacked plot. Below is the code and it is throwing an error. could you please help.

1 Like

If you look at the y-values in plot, they are not in order. Most likely their type is not number. You can confirm the type by printing subData.dtypes?

If not numeric, you can convert ‘subData’ to numeric and then plot again.

subData = subData.apply(pd.to_numeric)
1 Like

It worked. Thanks for the reply.

1 Like

The error “only length-1 arrays can be converted to Python scalars” is raised when the function expects a single value but you pass an array instead. This means that when you’re trying to cast into an integer something that isn’t just one scalar. Once you will look at the call signature of np.int, you’ll see that it accepts a single value, not an array.

What you can do is use .astype(int)