title: In python how would you create two columns that iterate into eachother?
body: <p>The question translate into this I have a simple dataframe lets call it df</p>
  <pre><code>id  k   t
  2A  1   100
  2A  2   100
  2A  NaN 100
  4C  3   80
  4C  4   80
  4C  1   80
  3B  NaN 120
  3B  7   120
  3B  4   120
  </code></pre>
  <p>And I want to create a new column that is called t and t1. t1 is based on t and k and id. In this scenario I have only 3 Id's 2A, 4C and 3B.  The final output will be.</p>
  <pre><code>id  k   t   t1
  2A  1   100 99
  2A  2   99  97
  2A  NaN 97  97
  4C  3   80  77
  4C  4   77  73
  4C  1   73  72
  3B  NaN 120 120
  3B  7   120 113
  3B  4   113 109
  </code></pre>
  <p>So the idea is as follows</p>
  <p><a href="https://i.stack.imgur.com/YHQlL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YHQlL.png" alt="enter image description here" /></a></p>
  <p>in Which t1 is simply</p>
  <pre><code>df['t1']=df['t']-df['k']
  </code></pre>
  <p>The conversion of values on t in the final df is the one that I don't know how to do because the first value of every id has to be based on the originals <code>df[&quot;t&quot;]</code>, but afterwards on df[t1] as the picture shows.</p>