在 Pandas Grouby 数据框上建立索引给出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30018342/
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-09-13 23:18:16  来源:igfitidea点击:

Indexing on Pandas Grouby Data frame Gives error

pandas

提问by function

I have a Pandas GroupBy Data frame named ratings_by_titlethat looks like the following:

我有一个名为ratings_by_title的 Pandas GroupBy 数据框,如下所示:

title
,000,000 Duck (1971)                37
'Night Mother (1986)                  70
'Til There Was You (1997)             52
'burbs, The (1989)                   303
...And Justice for All (1979)        199
1-900 (1994)                           2
10 Things I Hate About You (1999)    700
101 Dalmatians (1961)                565
101 Dalmatians (1996)                364
12 Angry Men (1957)                  616

I am trying to filter out the titles having a rating of >=250 so,

我试图过滤掉评分 >=250 的标题,所以,

I tried the following active_titles = ratings_by_title.index[ratings_by_title >= 250]

我尝试了以下 active_titles = ratings_by_title.index[ratings_by_title >= 250]

But,This gives an error in iPython saying

但是,这在 iPython 中给出了一个错误说

AttributeError: Cannot access attribute 'index' of 'DataFrameGroupBy' objects, try using the 'apply' method

AttributeError:无法访问“DataFrameGroupBy”对象的属性“index”,请尝试使用“apply”方法

Could somebody help me understand what's going on?

有人可以帮助我了解发生了什么吗?

回答by function

Got it ... when grouping by should add the size method

明白了......分组时应该添加大小方法

eg) ratings_by_title = data.groupby('title').size()

例如) ratings_by_title = data.groupby('title').size()

This solved the issue!!

这就解决了问题!!

Now i can index like:

现在我可以索引如下:

active_ratings = ratings_by_title.index[ratings_by_title >= 250]

active_ratings = ratings_by_title.index[ ratings_by_title >= 250]