how to remove index column from multi index dataframe

0

Issue

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?

Solution

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

Answered By – jezrael

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More