-1

I have a DataFrame multiData that looks like this:

print(multiData)

                 Date    Open    High     Low   Close  Adj Close     Volume
Ticker Date                                                                 
AAPL   0    2010-01-04    7.62    7.66    7.59    7.64       6.51  493729600
       1    2010-01-05    7.66    7.70    7.62    7.66       6.52  601904800
       2    2010-01-06    7.66    7.69    7.53    7.53       6.41  552160000
       3    2010-01-07    7.56    7.57    7.47    7.52       6.40  477131200
       4    2010-01-08    7.51    7.57    7.47    7.57       6.44  447610800
...                ...     ...     ...     ...     ...        ...        ...
META   2668 2022-12-23  116.03  118.18  115.54  118.04     118.04   17796600
       2669 2022-12-27  117.93  118.60  116.05  116.88     116.88   21392300
       2670 2022-12-28  116.25  118.15  115.51  115.62     115.62   19612500
       2671 2022-12-29  116.40  121.03  115.77  120.26     120.26   22366200
       2672 2022-12-30  118.16  120.42  117.74  120.34     120.34   19492100

I need to get rid of "Date 0, 1, 2, ..." column and make the actual "Date" column part of the (multi) index

How do I do this?

Ivan
  • 7,448
  • 14
  • 69
  • 134
  • Does this answer your question? 1) [How to remove levels from a multi-indexed dataframe?](https://stackoverflow.com/questions/17084579/how-to-remove-levels-from-a-multi-indexed-dataframe) specifically, [this answer using `df.droplevel()`](/a/68018461/4518341). 2) [Convert a column to an index in pandas](https://stackoverflow.com/questions/27062908/convert-a-column-to-an-index-in-pandas) – wjandrea Feb 19 '23 at 23:59

1 Answers1

0

Use df.droplevel to delete level 1 and chain df.set_index to add column Date to the index by setting the append parameter to True.

df = df.droplevel(1).set_index('Date', append=True)
df

                   Open  High   Low  Close  Adj Close     Volume
Ticker Date                                                     
AAPL   2010-01-04  7.62  7.66  7.59   7.64       6.51  493729600
       2010-01-05  7.66  7.70  7.62   7.66       6.52  601904800
ouroboros1
  • 9,113
  • 3
  • 7
  • 26