How to melt 2 columns at the same time?

后端 未结 2 1761
遇见更好的自我
遇见更好的自我 2020-11-27 23:16

In Pandas, I have the following data frame:

   id1 id2 t1  l1  t2  l2 
0  1   2   a   b   c   d
1  3   4   g   h   i   j

I would like to me

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 23:44

    This is wide_to_long

    pd.wide_to_long(df,['t','l'],i=['id1','id2'],j='drop').reset_index(level=[0,1])
    Out[52]: 
          id1  id2  t  l
    drop                
    1       1    2  a  b
    2       1    2  c  d
    1       3    4  g  h
    2       3    4  i  j
    

提交回复
热议问题