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

前端 未结 3 1121
庸人自扰
庸人自扰 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:23

    A possibly simpler solution still using .melt() is to pull your date index out into a column with .reset_index() first:

    sim_2005_melted = pd.melt(sim_2005.reset_index(), id_vars=sim_2005.index.name, value_vars=list(sim_2005.columns.values), var_name='ELEM', value_name='Q_sim')

    You get the same result with .stack() but this way is a bit more flexible if you want all the extra melty goodness.

提交回复
热议问题