python - plt.hist errors on subsetted data -


i'm new python please excuse stupidity on part.

i'm running histogram matplotlib , getting errors when use subset data, code works if use full dataset, hence confusion.

perhaps i'm not subsetting correctly?

my code below , related errors below, thanks.

  • for awareness, written in python 3.

import required packages:

import numpy np  import matplotlib.pyplot plt  import seaborn sns 

read data:

mlb=pd.read_csv('c:\users\ocmh\desktop\python\batting.csv') 

view sample of data:

mlb.head() 

subset data return boston data:

mlb_bos=mlb[(mlb['teamid'] == 'bos')] 

view sample of subset data:

mlb_bos.head() 

plot histogram of original data: , works perfectly

plt.hist(mlb.ab.dropna, color= sns.desaturate("indianred",1)) 

plot histogram of subset data: returns errors (errors below)

plt.hist(mlb_bos.ab.dropna, color= sns.desaturate("indianred",1)) 

if don't have seaborn package installed, can drop color= sns.desaturate("indianred",1) purely aesthetics.

errors below:


keyerror                                  traceback (most recent call last)  <ipython-input-11-1484047d7ac6> in <module>()  ----> 1 plt.hist(mlb_bos.ab, color=color)    /users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)     2894                       histtype=histtype, align=align, orientation=orientation,     2895                       rwidth=rwidth, log=log, color=color, label=label,  -> 2896                       stacked=stacked, **kwargs)     2897         draw_if_interactive()     2898     finally:    /users/mattoconnell/anaconda/lib/python3.4/site-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)     5602         # massage 'x' processing.     5603         # note: sure changes here done below 'weights'  -> 5604         if isinstance(x, np.ndarray) or not iterable(x[0]):     5605             # todo: support masked arrays;     5606             x = np.asarray(x)    /users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/series.py in __getitem__(self, key)      512     def __getitem__(self, key):      513         try:  --> 514             result = self.index.get_value(self, key)      515      516             if not np.isscalar(result):    /users/mattoconnell/anaconda/lib/python3.4/site-packages/pandas/core/index.py in get_value(self, series, key)     1458     1459         try:  -> 1460             return self._engine.get_value(s, k)     1461         except keyerror e1:     1462             if len(self) > 0 , self.inferred_type in ['integer','boolean']:    pandas/index.pyx in pandas.index.indexengine.get_value (pandas/index.c:3113)()    pandas/index.pyx in pandas.index.indexengine.get_value (pandas/index.c:2844)()    pandas/index.pyx in pandas.index.indexengine.get_loc (pandas/index.c:3704)()    pandas/hashtable.pyx in pandas.hashtable.int64hashtable.get_item (pandas/hashtable.c:7255)()    pandas/hashtable.pyx in pandas.hashtable.int64hashtable.get_item (pandas/hashtable.c:7193)()    keyerror: 0 


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -