Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Indexing on Pandas Grouby Data frame Gives error

Tags:

pandas

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?

like image 884
function Avatar asked May 03 '15 19:05

function


1 Answers

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]

like image 199
function Avatar answered Jan 04 '23 05:01

function