Home > database >  How to set group by column-name as column instead of index?(Pandas)
How to set group by column-name as column instead of index?(Pandas)

Time:07-30

I have a dataframe:

grouped_df = df.groupby("DATE").sum()

The dataframe looks like:

enter image description here

The group by column DATE is shown as index here. I want the column DATE to be a separate column rather than index.

The condition is that grouped_df['DATE'] should display the data instead of an error.

CodePudding user response:

You can use

grouped_df = df.groupby("DATE", as_index=False).sum()
# or
grouped_df = df.groupby("DATE").sum().reset_index()
  • Related