Pandas - Self reference of instances in column
<p>I have the following DF</p>

<pre><code> SampleID ParentID
0  S10        S20    
1  S10        S30    
2  S20        S40     
3  S30              
4  S40       
</code></pre>

<p>How can I put the id of the other row in the column 'ParentID' instead of the string?</p>

<p>Expected result:</p>

<pre><code> SampleID ParentID 
0  S10        2    
1  S10        3    
2  S20        4   
3  S30              
4  S40              
</code></pre>

<p>The closest result I found for this problem was:
<a href="http://stackoverflow.com/questions/28101317/how-to-self-reference-column-in-pandas-data-frame">How to self-reference column in pandas Data Frame?</a></p>

<p>Use <code>replace</code> by passing along the mapping lists of values to replace:</p>

<pre><code>df.ParentID.replace(df.SampleID.tolist(), df.index.tolist(), inplace=True)

df
Out[22]: 
  SampleID  ParentID
0      S10       2.0
1      S10       3.0
2      S20       4.0
3      S30       NaN
4      S40       NaN
</code></pre>

python pandas