Drop level for index

645 views Asked by At

I have the below result from a pivot table, which is about the count of customer grades that visited my stores. I used the 'droplevel' method to flatten the column header into 1 layer, how can I do the same for the index? I want to remove 'Grade' above the index, so that the column headers are at the same level as 'Store No_'.

enter image description here

1

There are 1 answers

0
jezrael On BEST ANSWER

it seems you need remove column name:

df.columns.name = None

Or rename_axis:

df = df.rename_axis(None, axis=1)

Sample:

df = pd.DataFrame({'Store No_':[1,2,3],
                   'A':[4,5,6],
                   'B':[7,8,9],
                   'C':[1,3,5],
                   'D':[5,3,6],
                   'E':[7,4,3]})
df = df.set_index('Store No_')
df.columns.name = 'Grade'
print (df)
Grade      A  B  C  D  E
Store No_               
1          4  7  1  5  7
2          5  8  3  3  4
3          6  9  5  6  3

print (df.rename_axis(None, axis=1))
           A  B  C  D  E
Store No_               
1          4  7  1  5  7
2          5  8  3  3  4
3          6  9  5  6  3

df = df.rename_axis(None, axis=1).reset_index()
print (df)
   Store No_  A  B  C  D  E
0          1  4  7  1  5  7
1          2  5  8  3  3  4
2          3  6  9  5  6  3