How to access column after pandas .groupby

Viewed 13212

I have a data frame that I used the .groupby() along with .agg() function on.

movieProperties = combined_df.groupby(['movieId', 'title', 'genres']).agg({'rating': ['count', 'mean']})

This is the code to create the new data frame. However I can't seem to access columns the same way anymore. If I try movieProperties['genres'] I always get a KeyError. How can I access columns again in this new data frame?

1 Answers

after you groupby, the columns you grouped by are now called index:

movieProperties = pd.DataFrame({"movie": ["x", "x", "y"], "title":["tx", "tx", "ty"], "rating": [3, 4, 3]}).groupby(["movie", "title"]).agg({"rating":["count", "mean"]})
movieProperties.index.values
Out[13]: array([('x', 'tx'), ('y', 'ty')], dtype=object)

if you're not comfortable with that, reset them back to regular columns:

movieProperties.reset_index()
Out[16]: 
  movie title rating     
               count mean
0     x    tx      2  3.5
1     y    ty      1  3.0

and then

movieProperties.reset_index()["movie"]
Out[17]: 
0    x
1    y
Related