Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a smart way to do it?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading