Hannah Sawyer
Hannah Sawyer

Reputation: 33

"TypeError: cannot convert the series to <type 'float'>" when plotting pandas series data

I'm getting an error when trying to plot a bar graph using matplotlib.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
source_data= pd.read_csv("ehresp_2015.csv")

then I extract the two columns from the data set that I need

results_1 = source_data[["EUGROSHP","EUGENHTH"]]

get rid of the negative values

results_1_new = results_1[results_1>0]

plot the data

x=results_1_new['EUGROSHP']
y=results_1_new['EUGENHTH']
plt.bar([x],[y])
plt.show()

I'm getting an error TypeError: cannot convert the series to

Upvotes: 0

Views: 1192

Answers (2)

cs95
cs95

Reputation: 402363

Use df.plot, should be easier.

source_data.set_index("EUGROSHP")["EUGENHTH"].plot(kind='bar', legend=True)
plt.show()

Also, note that results_1[results_1>0] will give you a bunch of NaNs in your columns, did you mean to filter on a single column instead?

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339170

You have the Series to plot encapsulated in a list, plt.bar([x],[y]. This way you would ask matplotlib to plot exactly one bar at position x and height y. Since x, y are no numerical values, but Series themselves, this is of course not possible and results in the error TypeError: cannot convert the series to <type 'float'>

The solution is quite simple, don't put the Series into lists, but leave them as Series:

plt.bar(x,y)



Just to show you how a minimal verifiable example which you may use when asking a question could look like, here a complete code:

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import pandas as pd

fig, axes = plt.subplots()
data = pd.DataFrame({"a":np.arange(10)-2,"b":np.random.randn(10)})

results_1 = data[["a","b"]]
results_1_new = results_1[results_1>0]

x=results_1_new['a']
y=results_1_new['b']
plt.bar(x,y)
plt.show()

enter image description here

Upvotes: 1

Related Questions