找到你要的答案

Q:Indexing on Pandas Grouby Data frame Gives error

Q:大熊猫的grouby数据帧索引给出错误

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

title
$1,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,

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

But,This gives an error in iPython saying

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

Could somebody help me understand what's going on?

我有一只熊猫分组数据帧命名ratings_by_title看起来如下:

title
$1,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,

我尝试了以下active_titles = ratings_by_title。指数[ ratings_by_title >;= 250 ]

但是,这给了一个错误,IPython说

意思是:无法访问属性指数'的' dataframegroupby对象,尝试使用“应用”的方法

有谁能帮我明白发生了什么事吗?

answer1: 回答1:

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

eg) 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]

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

EG)ratings_by_title =数据。GroupBy(标题)。size()

这解决了这个问题!!

现在我可以像指数:

active_ratings = ratings_by_title。指数[ ratings_by_title >;= 250 ]

pandas