how to remove index column from multi index dataframe

Viewed 482

I've seen a lot of answers on flattening multi-level index. I just want to remove the very first index column on the left of this multi-index dataframe.

From this:

Attributes       Date    Close    Close               
Symbols                   AMZN      ^DJI  
0          2020-12-01  3220.08  29823.92  
1          2020-11-30  3168.04  29638.64  
2          2020-11-27  3195.34  29910.37  
3          2020-11-25  3185.07  29872.47

I'm looking for this outcome:

       Date    Close    Close               
               AMZN      ^DJI  
2020-12-01  3220.08  29823.92  
2020-11-30  3168.04  29638.64  
2020-11-27  3195.34  29910.37  
2020-11-25  3185.07  29872.47

Is this possible?

2 Answers

If need set first MultiIndex column to index use DataFrame.set_index with rename index and columns names by DataFrame.rename_axis - so output is DataFrame with DatetimeIndex:

df = df.set_index([('Date', '')]).rename_axis(index=None, columns=('Date',''))
print (df)
Date          Close          
               AMZN      ^DJI
2020-12-01  3220.08  29823.92
2020-11-30  3168.04  29638.64
2020-11-27  3195.34  29910.37
2020-11-25  3185.07  29872.47

If I've understood correctly, this is easiest way to do it:

df.reset_index().drop(columns="Attributes", axis = 1)
Related