Monica Monica - 3 months ago 9
Python Question

Removing the name of mulilevel index

I created the mulitlevel DataFrame:

Price
Country England Germany US
sys dis
23 0.8 300.0 300.0 800.0
24 0.8 1600.0 600.0 600.0
27 1.0 4000.0 4000.0 5500.0
30 1.0 1000.0 3000.0 1000.0


Right now I want to remove the name: Country, and add the index from 0 to..

Price
sys dis England Germany US
0 23 0.8 300.0 300.0 800.0
1 24 0.8 1600.0 600.0 600.0
2 27 1.0 4000.0 4000.0 5500.0
3 30 1.0 1000.0 3000.0 1000.0


This is my code:

df = pd.DataFrame({'sys':[23,24,27,30],'dis': [0.8, 0.8, 1.0,1.0], 'Country':['US', 'England', 'US', 'Germany'], 'Price':[500, 1000, 1500, 2000]})


df = df.set_index(['sys','dis', 'Country']).unstack().fillna(0)


Can I have some hints how to solve it? I don't have too much experience with multilevel DataFrame.

Answer

Best I've got for now:

df.rename_axis([None, None], 1).reset_index()

enter image description here