1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.