I don\'t understand the output of pandas\' groupby. I started with a DataFrame (df0
) with 5 fields/columns (zip, city, location, population, state).
<
It's hard to say definitively without sample data, but with the code you show, returning a Series, you should be able to access the population for a city by using something like df6.loc['Albany', 'NY']
(that is, index your grouped Series by the city and state).
The reason you get a Series is because you selected a single column ('pop')
on which to apply your group computation. If you apply your group computation to a list of columns, you'll get a DataFrame. You could do this by doing df6 = df0.groupby(['city','state'])[['pop']].sum()
. (Note the extra brackets around 'pop'
, to select a list of one column instead of a single column.) But I'm not sure there's a reason to do this if you can use the above method to access the city data anyway.