0

I have a data frame, df, that doesn't stack properly. I would believe that assigning my product of the groupby() function for Pandas dataframe would output a dataframe, but instead I get a pandas.core.groupby.DataFrameGroupBy object.

Or, can you convert the object into a dataframe?

Here is my input, df:

                  Flavor      MONTH        Score
6                 Vanilla     March_2018   2.941176
7                 Chocolate   March_2018   7.692308
8                 Banana      March_2018   0.000000
23                Strawberry  March_2018   20.000000

Here is my command: Score_df = df1.groupby(['Flavor','MONTH'])

And I want to produce:

Score_df =

MONTH         March_2018
Flavor
Vanilla       2.941176
Chocolate     7.692308
Banana        0.000000
Strawberry    20.000000

but instead I get: <pandas.core.groupby.DataFrameGroupBy object at 0x000002B7C2BE45F8>

florence-y
  • 751
  • 3
  • 8
  • 18

1 Answers1

0

You can use:

In [210]: df.pivot_table(index=df.Flavor, values='Score', columns='MONTH')
     ...: 
Out[210]: 
MONTH       March_2018
Flavor                
Banana        0.000000
Chocolate     7.692308
Strawberry   20.000000
Vanilla       2.941176
llllllllll
  • 16,169
  • 4
  • 31
  • 54