Python Pandas Group By Error 'Index' 对象没有属性 'labels'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42400773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:38:59  来源:igfitidea点击:

Python Pandas Group By Error 'Index' object has no attribute 'labels'

pythonpandasdataframe

提问by Uasthana

Hi guys is am getting this error:

嗨,伙计们,我收到了这个错误:

 'Index' object has no attribute 'labels'

The traceback looks like this:

回溯看起来像这样:

Traceback (most recent call last):

 File "<ipython-input-23-e0f428cee427>", line 1, in <module>
df_top_f = k.groupby(['features'])['features'].count().unstack('features')

 File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 2061, in unstack
return unstack(self, level, fill_value)

File "C:\Anaconda3\lib\site-packages\pandas\core\reshape.py", line 405, in unstack
fill_value=fill_value)

File "C:\Anaconda3\lib\site-packages\pandas\core\reshape.py", line 90, in __init__
self.lift = 1 if -1 in self.index.labels[self.level] else 0

AttributeError: 'Index' object has no attribute 'labels'

While running the following code

在运行以下代码时

df_top_f = df.groupby(['features'])['features'].count().unstack('features')

df has the following structure:

df 具有以下结构:

                                      features
             Ind                              
             0                         Doorman
             1                    Cats Allowed
             2                         Doorman
             3                    Cats Allowed
             4                    Dogs Allowed
             5                         Doorman

df.index looks like this:

df.index 看起来像这样:

RangeIndex(start=0, stop=267906, step=1, name='Ind')

Looks very straight forward but I can't understand why I am getting this error. Please help

看起来很直接,但我不明白为什么我会收到这个错误。请帮忙

采纳答案by miradulo

Perhaps not the shortest, but a very straightforward approach would just be to construct a new DataFrame explicitly from the index and values.

也许不是最短的,但一个非常简单的方法就是从索引和值中显式地构造一个新的 DataFrame。

>>> grp_cnt = df.groupby(['features'])['features'].count()
>>> pd.DataFrame(dict(features=grp_cnt.index, count=grp_cnt.values))

   count      features
0      2  Cats Allowed
1      1  Dogs Allowed
2      3       Doorman

Alternatively, you could achieve a one-liner with renaming columns and to_frameusing

或者,您可以通过重命名列并to_frame使用

>>> df.groupby(['features'])['features'].count().to_frame().rename(
         columns={'features':'counts'}).reset_index()

       features  counts
0  Cats Allowed       2
1  Dogs Allowed       1
2       Doorman       3

Your current attempt isn't working because you can't unstack a single level index on a Series to coerce it into a DataFrame.

您当前的尝试无效,因为您无法在 Series 上取消堆叠单级索引以将其强制转换为 DataFrame。