1

I am new to python/ pandas and trying to create a boxplot using the iris data set.

Here is my code.:

import pandas as pd
iris_filename = '/Users/pro/Documents/Code/Data Science/Iris/IRIS.csv'
    iris = pd.read_csv(iris_filename, header = None, 
names= ['sepal_lenght','sepal_width','petal_lenght','petal_width','target'])
plt.boxplot(iris)

I get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-e190e88674b0> in <module>()
----> 1 plt.boxplot(iris)

/anaconda/lib/python3.5/site-packages/matplotlib/pyplot.py in boxplot(x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder, hold, data)
   2784                          whiskerprops=whiskerprops,
   2785                          manage_xticks=manage_xticks, autorange=autorange,
-> 2786                          zorder=zorder, data=data)
   2787     finally:
   2788         ax._hold = washold

/anaconda/lib/python3.5/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1890                     warnings.warn(msg % (label_namer, func.__name__),
   1891                                   RuntimeWarning, stacklevel=2)
-> 1892             return func(ax, *args, **kwargs)
   1893         pre_doc = inner.__doc__
   1894         if pre_doc is None:

/anaconda/lib/python3.5/site-packages/matplotlib/axes/_axes.py in boxplot(self, x, notch, sym, vert, whis, positions, widths, patch_artist, bootstrap, usermedians, conf_intervals, meanline, showmeans, showcaps, showbox, showfliers, boxprops, labels, flierprops, medianprops, meanprops, capprops, whiskerprops, manage_xticks, autorange, zorder)
   3266             bootstrap = rcParams['boxplot.bootstrap']
   3267         bxpstats = cbook.boxplot_stats(x, whis=whis, bootstrap=bootstrap,
-> 3268                                        labels=labels, autorange=autorange)
   3269         if notch is None:
   3270             notch = rcParams['boxplot.notch']

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in boxplot_stats(X, whis, bootstrap, labels, autorange)
   1984 
   1985     # convert X to a list of lists
-> 1986     X = _reshape_2D(X)
   1987 
   1988     ncols = len(X)

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in _reshape_2D(X)
   2245                 X = [X.ravel()]
   2246             else:
-> 2247                 X = [X[:, i] for i in xrange(ncols)]
   2248         else:
   2249             raise ValueError("input `X` must have 2 or fewer dimensions")

/anaconda/lib/python3.5/site-packages/matplotlib/cbook.py in <listcomp>(.0)
   2245                 X = [X.ravel()]
   2246             else:
-> 2247                 X = [X[:, i] for i in xrange(ncols)]
   2248         else:
   2249             raise ValueError("input `X` must have 2 or fewer dimensions")

/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2057             return self._getitem_multilevel(key)
   2058         else:
-> 2059             return self._getitem_column(key)
   2060 
   2061     def _getitem_column(self, key):

/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2064         # get column
   2065         if self.columns.is_unique:
-> 2066             return self._get_item_cache(key)
   2067 
   2068         # duplicate columns & possible reduce dimensionality

/anaconda/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1382         """Return the cached item, item represents a label indexer."""
   1383         cache = self._item_cache
-> 1384         res = cache.get(item)
   1385         if res is None:
   1386             values = self._data.get(item)

TypeError: unhashable type: 'slice'

I haae searched the web for this and cannot seem to find answer for this issue. I will appreciate any help on this.

1 Answer 1

2

You are calling matplotlib to boxplot the DataFrame iris... as you are already using Pandas for importing the .csv you should also use it for plotting:

iris.boxplot()

Pandas boxplot api

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.