Nick
Nick

Reputation: 23

How to rename indexes generated by group by function?

I want to get a weekly mean of data for every year. So I am using groupby for week and year as in the below code:

hdfc.groupby([hdfc.Date.dt.year,hdfc.Date.dt.week]).mean()

enter image description here

How can I make week and year as the columns of the data? reset_index() is giving me an error "cannot insert Date, already exists" since both the columns have the name Date which is generated by groupby function.

Upvotes: 1

Views: 203

Answers (1)

Muhammad Hamza Sabir
Muhammad Hamza Sabir

Reputation: 345

Below given piece of code will output your desired result:

df.rename_axis(index=['Year', 'Week']).reset_index()

Upvotes: 1

Related Questions