Black Lives Matter. Please consider donating to Black Girls Code today.

Having trouble summing data

In matplotlib I generated a graph using this code:

df_pre_2003.groupby(['StateName']) 
['nAllNeonic'].sum().plot(kind='bar')
plt.title("Neonic usage by state prior to 2003")
plt.rcParams["figure.figsize"] = (10, 5)
plt.rcParams["xtick.labelsize"] = 10
plt.rcParams["ytick.labelsize"] = 10

Which gave me this:
Screenshot%20(80)

Now in plotly I have this code:
data=[go.Bar(x=df_2003.StateName, y=df_2003.nAllNeonic)]
iplot(data, filename=‘testbar’)

Which gives me something similar but not correct since the y is not summed. But, when I try to sum by adding y=df_2003.nAllNeonic).sum() it it says:

ValueError:
Invalid value of type ‘numpy.float64’ received for the ‘y’ property of bar
Received value: 50.97505208286809

The 'y' property is an array that may be specified as a tuple,
list, numpy array, or pandas Series

It’s probably a simple fix but, I seem to be having a hard time. Side note this is the first time I have used plotly.

Hi @iiWylde,

I think you want something like this:

df_summed = df_pre_2003.groupby(['StateName']) ['nAllNeonic'].sum().reset_index()
data=[go.Bar(x=df_summed.StateName, y=df_summed.nAllNeonic)]
iplot(data, filename=‘testbar’)

Hope that helps!
-Jon

Thank you very much! It worked!

1 Like