How can I convert columns to rows in Pandas?

前端 未结 3 1631
Happy的楠姐
Happy的楠姐 2021-01-23 16:48

I have something like this:

Values     Time
  22        0
  45        1
  65        2
  78        0
  12        1
  45        2

and I want this

3条回答
  •  北海茫月
    2021-01-23 17:09

    If your time-delta is constant, ordered and has no missing values:

    DELTA = 3
    new_values = [df['Values'].iloc[i*DELTA:i*DELTA+DELTA].values.transpose() for i in range(int(len(df)/DELTA))]
    df_new = pd.DataFrame(new_values , index=['Val'+str(i+1) for i in range(len(new_values ))])
    
    print(df_new)
             0   1   2
    Val1    22  45  65
    Val2    78  12  45
    

    Not a pretty solution, but maybe it helps. :-)

提交回复
热议问题