首页 文章

Python for Data Analysis:Chp 2 Pg 38“prop_cumsum”错误

提问于
浏览
0

当我运行“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:不推荐使用sort_index的参数,如果name =='main,请使用.sort_values(by = ...) ':------------------------------------------------ --------------------------- KeyError Traceback(最近一次调用最后一次)/Users/anaconda/lib/python3.5/site-packages/ get_loc中的pandas / indexes / base.py(self,key,method,tolerance)1944尝试: - > 1945返回self._engine.get_loc(key)1946除了KeyError:pandas.index.IndexEngine.get_loc中的pandas / index.pyx (pandas / index.c:4154)()pandas.index.IndexEngine.get_loc中的pandas / index.pyx(pandas / index.c:4018)()pandas.hashtable.PyObjectHashTable.get_item中的pandas / hashtable.pyx(pandas) /hashtable.c:12368)()pandas.hashtable.PyObjectHashTable.get_item中的pandas / hashtable.pyx(pandas / hashtable.c:12322)()KeyError:'prop'在处理上述异常期间,发生了另一个异常:KeyError跟踪(最近一次调用最后一次)在()----> 1 prop_cumsum = df.sort_index(by ='prop', sort_index中的升序=假).prop.cumsum()/Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py(self,axis,level,ascending,inplace,kind,na_position,sort_remaining ,by)3237引发ValueError(“无法同时排序和水平”)3238返回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如果k.ndim == 2:3153 /Users/anaconda/lib/python3.5/site-packages/pandas/core/frame.py in getitem(self,key)1995返回自我._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在_getitem_column(self,key)2002#get column 2003 if self.columns.is_unique: - > 2004 return self._get_item_c ache(key)2005 2006#duplicate columns&possible reduced dimensionality /Users/anaconda/lib/python3.5/site-packages/pandas/core/generic.py in _get_item_cache(self,item)1348 res = cache.get(item )1349如果res为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返回self._engine.get_loc(key)1946除了KeyError: - > 1947返回self._engine.get_loc(self._maybe_cast_indexer(key))1948 1949 indexer = self.get_indexer([key],method = method,tolerance = tolerance) Pandas.index.IndexEn中的pandas / index.pyx在pandas.index.IndexEngine.get_loc(pandas / index.c:4154)()pandas / index.pyx中gine.get_loc(pandas / index.c:4018)()pandas.hashtable.PyObjectHashTable.get_item中的pandas / hashtable.pyx(pandas / hashtable.c:12368)()pandas / hashtable.pyx在pandas.hashtable.PyObjectHashTable中 . get_item(pandas / hashtable.c:12322)()KeyError:'prop'

1 回答

  • 0

    您似乎调用了 sort_index 而不是 sort_values . by='prop' 在这样的上下文中没有意义(您按索引排序索引,而不是按数据框中的列排序) .

    此外,在我的第二版早期版本中,它出现在第43页的顶部附近 . 但由于这是早期版本,页码可能不稳定 .

相关问题