Pandas. Series throwing KeyError in pyplot.hist
I am generating Dataframe. I pull a row of floats out of it and plot it on a histogram. Works great.
But when I create a sub-series of this data using one of two descriptions:
u83 = results['Wilks'][results['Weight Class'] == 83]
u83 = results[results['Weight Class'] == 83]['Wilks']
pyplot.hist throws KeyError on this series.
#this works fine
plt.hist(results['Wilks'], bins=bins)
# type is <class 'pandas.core.series.Series'>
print(type(results['Wilks']))
# type is <type 'numpy.float64'>
print(type(results['Wilks'][0]))
#this histogram fails with a KeyError for both of these selectors:
u83 = results['Wilks'][results['Weight Class'] == 83]
u83 = results[results['Weight Class'] == 83]['Wilks']
print u83
#type is <class 'pandas.core.series.Series'>
print(type(u83))
#plt.hist(u83) fails with a KeyError
plt.hist(u83)
I just started messing around with Pandas. Maybe I'm not going to use the sql equivalent of "select * from table where Weightclass = 83" etc. correctly.
+3
source to share
2 answers
You probably want something like this:
u83 = results.loc[results['Weight Class'] == 83, 'Wilks']
plt.hist(u83)
And you can read these docs for indexing ...
0
source to share