prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()..." /> prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()..." /> prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()..."/>

Python for Data Analysis: Chp 2 Pg 38 "prop_cumsum" error

219 views Asked by At

I'm working on this book and keep running error when i'm run "Prop_cumsum"

> prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()

/Users/anaconda/lib/python3.5/site-packages/ipykernel/main.py:1: FutureWarning: by argument to sort_index is deprecated, pls use .sort_values(by=...) if name == 'main': --------------------------------------------------------------------------- KeyError Traceback (most recent call last) /Users/anaconda/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 1944 try: -> 1945 return self._engine.get_loc(key) 1946 except KeyError:

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

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

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'prop'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last) in () ----> 1 prop_cumsum = df.sort_index(by='prop', ascending=False).prop.cumsum()

/Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in sort_index(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, by) 3237 raise ValueError("unable to simultaneously sort by and level") 3238 return self.sort_values(by, axis=axis, ascending=ascending, -> 3239 inplace=inplace) 3240 3241 axis = self._get_axis_number(axis)

/Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in sort_values(self, by, axis, ascending, inplace, kind, na_position)
3149 3150 by = by[0] -> 3151 k = self[by].values 3152 if k.ndim == 2: 3153

/Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in getitem(self, key) 1995 return self._getitem_multilevel(key) 1996 else: -> 1997 return self._getitem_column(key) 1998 1999 def _getitem_column(self, key):

/Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_column(self, key) 2002 # get column 2003 if self.columns.is_unique: -> 2004 return self._get_item_cache(key) 2005 2006 # duplicate columns & possible reduce dimensionality

/Users/anaconda/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self, item) 1348 res = cache.get(item) 1349 if res is None: -> 1350 values = self._data.get(item) 1351 res = self._box_item_values(item, values) 1352
cache[item] = res

/Users/anaconda/lib/python3.5/site-packages/pandas/core/internals.py in get(self, item, fastpath) 3288 3289 if not isnull(item): -> 3290 loc = self.items.get_loc(item) 3291 else: 3292 indexer = np.arange(len(self.items))[isnull(self.items)]

/Users/anaconda/lib/python3.5/site-packages/pandas/indexes/base.py in get_loc(self, key, method, tolerance) 1945 return self._engine.get_loc(key) 1946 except KeyError: -> 1947 return self._engine.get_loc(self._maybe_cast_indexer(key)) 1948 1949
indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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

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

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)()

KeyError: 'prop'

1

There are 1 answers

0
pglezen On

It seems that you invoked sort_index instead of sort_values. The by='prop' doesn't make sense in such a context (you sort the index by the index, not by columns in the data frame).

Also, in my early release copy of the 2nd edition, this appears near the top of page 43. But since this is early release, the page numbers may be fluid.