Code Error in Python Community Tutorial

I noticed running the code below from the Python Community tutorial returns an error.

Tutorial link is

Category Topic: Mean

data.pivot_table(columns='unique_carrier', values='arr_delay', aggfunc=np.mean).sort_values(ascending=False)

There was a problem running this cell
TypeError sort_values() takes at least 2 arguments (2 given) 
TypeErrorTraceback (most recent call last)
<ipython-input-12-0c365c5ad5df> in <module>()
----> 1 data.pivot_table(columns='unique_carrier', values='arr_delay', aggfunc=np.mean).sort_values(ascending=False)

TypeError: sort_values() takes at least 2 arguments (2 given)

Now when I adjust the code to this:

data.pivot_table(columns='unique_carrier', values='arr_delay', aggfunc=np.mean).sort_values('unique_carrier', ascending=False)

I get this error on the console

There was a problem running this cell
KeyError 'unique_carrier' 
KeyErrorTraceback (most recent call last)
<ipython-input-13-c1885e7d48cb> in <module>()
----> 1 data.pivot_table(columns='unique_carrier', values='arr_delay', aggfunc=np.mean).sort_values('unique_carrier', ascending=False)

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in sort_values(self, by, axis, ascending, inplace, kind, na_position)
   3283             by = by[0]
-> 3284             k = self.xs(by, axis=other_axis).values
   3285             if k.ndim == 2:

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/generic.pyc in xs(self, key, axis, level, drop_level)
   1911         if axis == 1:
-> 1912             return self[key]
   1914         self._consolidate_inplace()

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   2060             return self._getitem_multilevel(key)
   2061         else:
-> 2062             return self._getitem_column(key)
   2064     def _getitem_column(self, key):

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_column(self, key)
   2067         # get column
   2068         if self.columns.is_unique:
-> 2069             return self._get_item_cache(key)
   2071         # duplicate columns & possible reduce dimensionality

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_item_cache(self, item)
   1532         res = cache.get(item)
   1533         if res is None:
-> 1534             values = self._data.get(item)
   1535             res = self._box_item_values(item, values)
   1536             cache[item] = res

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/internals.pyc in get(self, item, fastpath)
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in get_loc(self, key, method, tolerance)
   2393                 return self._engine.get_loc(key)
   2394             except KeyError:
-> 2395                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2397         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5239)()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20405)()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20359)()

KeyError: 'unique_carrier'


The second error, I really do not understand why.

I added .stack() before the .sort_value and it worked but slightly different from what the solution is on the tutorial page

data.pivot_table(columns='unique_carrier', values='arr_delay', aggfunc=np.mean).stack().sort_values(ascending=False)


Any thoughts or comments

1reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Zion,

    If you type in the following code, it should run correctly:

    data.pivot_table(columns=['unique_carrier'], values='arr_delay').T.sort_values(['arr_delay'],ascending=False)

    I cloned the Python tutorial report and added this code in it. You can take a look at it here:

    Hope this helps!

    Reply Like
Like Follow
  • Status Answered
  • 10 mths agoLast active
  • 1Replies
  • 299Views
  • 2 Following