Home > front end >  Pandas : Create columns based on values of another column if string value from 3rd column
Pandas : Create columns based on values of another column if string value from 3rd column

Time:05-20

My dataframe is this :

position        labels
[58.0, 71.0]    ind    
[137.0, 147.0]  pro         
[170.0, 191.0]  pro          
[nan, nan]      NaN               
[nan, nan]      NaN               
[36.0, 57.0]    pro        
[67.0, 73.0]    ind     
[86.0, 93.0]    tar          
[0.0, 8.0]      ind     
   

The wanted output is this:

ind.position   pro.position   tar.position   
[58.0, 71.0]            
              [137.0, 147.0]       
              [170.0, 191.0]           
              [36.0, 57.0]        
[67.0, 73.0]  
                              [86.0, 93.0]               
[0.0, 8.0]              

So, based on the labels column, create 3 new columns with suffix the label value and endfix .position and use as values the corresponding position based on the label.

Is there a smart way to do it?

CodePudding user response:

Use DataFrame.dropna for remove original column with missing values, then convert index to column, so possible use DataFrame.pivot, last add DataFrame.add_suffix:

df = (df.dropna(subset=['labels'])
        .reset_index()
        .pivot('index','labels','position')
        .add_suffix('.position'))

print (df)
labels ind.position   pro.position tar.position
index                                          
0       [58.0,71.0]            NaN          NaN
1               NaN  [137.0,147.0]          NaN
2               NaN  [170.0,191.0]          NaN
5               NaN    [36.0,57.0]          NaN
6       [67.0,73.0]            NaN          NaN
7               NaN            NaN  [86.0,93.0]
8         [0.0,8.0]            NaN          NaN
  • Related