Python pandas - pd.melt a dataframe with datetime index results in NaN

前端 未结 3 1119
庸人自扰
庸人自扰 2021-01-14 16:37

I have the following dataframe (sim_2005):

Date         ELEM1 ELEM2 ... ELEM1133
2005-01-01   0.021 2.455 ... 345.2
2005-01-02   0.321 2.331 ... 355.1
...            


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 17:15

    Assuming Date is the index to your DataFrame, you can get a date column in your melted DataFrame as follows:

    sim_2005_melted['Date'] = pd.concat([sim_2005.reset_index().Date 
                                         for _ in range(sim_2005.shape[1])], 
                                        ignore_index=True).values
    

提交回复
热议问题