向数据框添加标题

Add a title to a dataframe

我原来有一个数据框df1,

                 Close                                                    
ticker            AAPL        AMD        BIDU        GOOGL          IXIC   
Date                                                                       
2011-06-01   12.339643   8.370000  132.470001   263.063049   2769.189941   
2011-06-02   12.360714   8.240000  138.490005   264.294281   2773.310059   
2011-06-03   12.265714   7.970000  133.210007   261.801788   2732.780029   
2011-06-06   12.072857   7.800000  126.970001   260.790802   2702.560059   
2011-06-07   11.858571   7.710000  124.820000   259.774780   2701.560059   
...                ...        ...         ...          ...           ...   
2021-05-24  127.099998  77.440002  188.960007  2361.040039  13661.169922   
2021-05-25  126.900002  77.860001  192.770004  2362.870117  13657.169922   
2021-05-26  126.849998  78.339996  194.880005  2380.310059  13738.000000   
2021-05-27  125.279999  78.419998  194.809998  2362.679932  13736.280273   
2021-05-28  124.610001  80.080002  196.270004  2356.850098  13748.740234   

由于计算的需要,我更改了列并创建了df2,其中不包含Close

ticker          AAPL       AMD      BIDU     GOOGL      IXIC
Date                                                                     
2011-08-25  0.760119  0.028203  0.621415  0.036067  0.993046
2011-09-23  0.648490  0.216017  0.267167  0.699657  0.562897
2011-10-21  0.442864  0.326310  0.197121  0.399332  0.048258
2011-11-18  0.333015  0.062089  0.164588  0.373293  0.015258
2011-12-19  0.101208  0.389120  0.218844  0.094759  0.116979
...              ...       ...       ...       ...       ...
2021-01-12  0.437177  0.012871  0.997870  0.075802  0.137392
2021-02-10  0.064343  0.178901  0.522356  0.625447  0.320007
2021-03-11  0.135033  0.300345  0.630085  0.253857  0.466884
2021-04-09  0.358583  0.484004  0.295894  0.215424  0.454395
2021-05-07  0.124987  0.311816  0.999940  0.232552  0.281189 

现在我正在努力研究如何再次向数据框添加名称,比如 ret,因为我想绘制每一列的直方图,并且希望标题类似于 ('ret', 'AAPL')...

这可能有点愚蠢和令人困惑,希望我已经清楚地解释了这个问题。感谢您的帮助。

你可以使用pd.MultiIndex.from_product()方法:

df2=df2.set_index('Date')
#If 'Date' column is not your Index then make it index
df2.columns=pd.MultiIndex.from_product([['ret'],df2.columns])