pandas 透视表bug "InvalidIndexError: Reindexing only valid with uniquely valued Index objects"

执行以下语句:

sbnew_feat.pivot_table(index=['prov_area'], columns=['city_tier'], aggfunc={'userid':'count'})

报错信息如下:

InvalidIndexErrorTraceback (most recent call last)
 in ()
----> 1 sbnew_feat.pivot_table(index=['prov_area'], columns=['city_tier'], aggfunc={'userid':'count'})

/Users/liyuan21/anaconda2/lib/python2.7/site-packages/pandas/core/frame.pyc in pivot_table(self, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
   5757                            aggfunc=aggfunc, fill_value=fill_value,
   5758                            margins=margins, dropna=dropna,
-> 5759                            margins_name=margins_name)
   5760 
   5761     def stack(self, level=-1, dropna=True):

/Users/liyuan21/anaconda2/lib/python2.7/site-packages/pandas/core/reshape/pivot.pyc in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
     74         for key in keys:
     75             try:
---> 76                 values = values.drop(key)
     77             except (TypeError, ValueError, KeyError):
     78                 pass

/Users/liyuan21/anaconda2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in drop(self, labels, errors)
   4958         arr_dtype = 'object' if self.dtype == 'object' else None
   4959         labels = com.index_labels_to_array(labels, dtype=arr_dtype)
-> 4960         indexer = self.get_indexer(labels)
   4961         mask = indexer == -1
   4962         if mask.any():

/Users/liyuan21/anaconda2/lib/python2.7/site-packages/pandas/core/indexes/base.pyc in get_indexer(self, target, method, limit, tolerance)
   2738 
   2739         if not self.is_unique:
-> 2740             raise InvalidIndexError('Reindexing only valid with uniquely'
   2741                                     ' valued Index objects')
   2742 

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

解决办法:

列过多,筛选目标列再操作。

sbnew_feat.loc[:, ['prov_area', 'city_tier', 'userid']].pivot_table(index=['prov_area'], columns=['city_tier'], aggfunc={'userid':'count'})

 

你可能感兴趣的:(python,算法)