0

I wanted to perform groupby and agg fucntion on my dataframe, so i performed the below code

basic_df = df.groupby(['S2PName','S2PName-Category'], sort=False)['S2PGTotal'].agg([('totSale','sum'), ('count','size')])

basic_df.head(2)

My O/P:

                              totSale               count
S2PName     S2PName-Category        
IDLY         Food                 598771.47             19749
DOSA          Food                567431.03             14611

Now I try to print the columns using basic_df.columns

My O/P:

Index(['totSale', 'count'], dtype='object')

Why are the other two columns "S2pname and S2PName-category" not being displayed. What do I need to do to display them as well?

Thanks !

Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46

1 Answers1

1

Adding as_index=False, or reset_index() at the end

basic_df = df.groupby(['S2PName','S2PName-Category'], sort=False,as_index=False)['S2PGTotal'].agg([('totSale','sum'), ('count','size')])

#basic_df = df.groupby(['S2PName','S2PName-Category'], sort=False)['S2PGTotal'].agg([('totSale','sum'), ('count','size')]).reset_index()
BENY
  • 317,841
  • 20
  • 164
  • 234