How can I get pandas' groupby command to return a DataFrame instead of a Series?

后端 未结 2 823
逝去的感伤
逝去的感伤 2021-01-19 15:56

I don\'t understand the output of pandas\' groupby. I started with a DataFrame (df0) with 5 fields/columns (zip, city, location, population, state).

<         


        
2条回答
  •  面向向阳花
    2021-01-19 16:19

    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.

提交回复
热议问题