0

here I had solved one my problem: Pandas duplicates groupby

Now, after using the command:

df.groupby('Names').agg({'Column1':'sum', 'Column2':'sum','Column3':'min'})

I have this DataFrame (I create an example):

Names Column1 Column2 Column3       
Bob     3     3        2011
John    3     3        2005
Jonh    1     2        2016
Pier    1     1        2003

But if I use the command df.columns, 'Names' is not displayed anymore. what can I do to have the column 'Names' as before using groupby?

cs95
  • 379,657
  • 97
  • 704
  • 746
user254087
  • 51
  • 5

1 Answers1

2

Specify parameter as_index=False while grouping:

df.groupby('Names', as_index=False).agg(
    {'Column1':'sum', 'Column2':'sum','Column3':'min'})

  Names  Column1  Column2  Column3
0   Bob        3        3     2011
1  John        4        5     2005
2  Pier        1        1     2003
cs95
  • 379,657
  • 97
  • 704
  • 746