Nikko
Nikko

Reputation: 1572

Pandas: reset_index after pd.PeriodIndex

Hi I am having problem with resetting the index after I made the columns groupby and PeriodIndex.

City_Zhvi_AllHomes.csv

housing = pd.read_csv('City_Zhvi_AllHomes.csv')
housing.drop(housing.columns[[0, 3, 4, 5]], axis=1, inplace=True)
housing.replace({'State': states}, inplace=True)
housing.set_index(['State', 'RegionName'], inplace=True)
housing.drop(housing.columns[housing.columns < '2000' ].tolist(), axis=1, inplace=True)
housing = housing.groupby(pd.PeriodIndex(housing.columns, freq='Q'), axis=1).mean()

I had to make some multi-index so that I can make some frequency and change the rest of the columns, after I achieved that, I wanted to reset the index.

housing.reset_index()

This function doesn't return an error, however it does not make changes and the multi-index remained.

I also can't add some columns with the dataframe i.e.:

housing['new_column'] = None

But I want to reset all so I can manipulate somethings again.

Upvotes: 0

Views: 288

Answers (1)

DYZ
DYZ

Reputation: 57033

housing.reset_index() returns a copy of your dataframe. Either save this copy:

housing = housing.reset_index()

or request the modification in place:

housing.reset_index(inplace=True)

Upvotes: 1

Related Questions