Home > Software design >  Reshaping Panda Data Frame and Create New Columns Based On a Column
Reshaping Panda Data Frame and Create New Columns Based On a Column

Time:08-20

I want to reshape a data frame like the following:

df1 = pd.DataFrame( columns=['Serial','Seq_Sp','PT','FirstPT','DiffAngle','R1'], 
              data=[['1001W','2_1',15.13,15.07,1.9,7.4], ['1001W','2_2',16.02,15.80,0.0,0.05],
                    ['1001W','2_3',14.3,15.3,6,0.32],['1001W','2_4',14.18,15.07,2.2,0.16],
                    ['6279W','2_1',15.13,15.13,2.3,0.31],['6279W','2_2',13.01,15.04,1.3,0.04],
                    ['6279W','2_3',14.13,17.04,2.3,0.31],['6279W','2_4',14.01,17.23,3.1,1.17]
                   ])
display(df1)

And create new one with unique serial numbers and long vector of new columns, like the following:

df2 = pd.DataFrame( columns=['Serial','PT_2_1','FirstPT_2_1','DiffAngle_2_1','R1_2_1','PT_2_2','FirstPT_2_2','DiffAngle_2_2',
                             'R1_2_2','PT_2_3','FirstPT_2_3','DiffAngle_2_3','R1_2_3','PT_2_4','FirstPT_2_4','DiffAngle_2_4',
                             'R1_2_4'],
                   data=[
                       ['1001W',15.13,15.07,1.9,7.4,16.02,15.80,0.0,0.05, 14.3,15.3,6,0.32 ,14.18,15.07,2.2,0.16],
                       ['6279W',15.13,15.13,2.3,0.31,13.01,15.04,1.3,0.04,14.13,17.04,2.3,0.31,14.01,17.23,3.1,1.17]
                   ])
df2

I appreciate any help!

enter image description here

enter image description here

CodePudding user response:

use enter image description here

  • Related