I have the following df with index1 year and index2 type.
data
year type
2017 A 1.23
B 5.51
2018 A 1.53
B 0.89
I want to rename the index year so that it says Year 0 and Year 1.
I tried the following:
index_years = []
for i in range(len(df.index.levels[0])):
index_years.append("Year " + str(i))
df.index.levels[0] = index_years
But I get the following error:
ERROR GIVEN: 'FrozenList' does not support mutable operations.
Then I tried this
pd.MultiIndex.from_tuples([(x[0].apply("Year " + str(i) for i in range(len(df.index.levels[0]))), x[1]) for x in df.index])
But it gives an error "int" boject is not subscriptible
The result I want to obtain is:
data
year type
Year 0 A 1.23
B 5.51
Year 1 A 1.53
B 0.89