title: Using values in the next row of a dataframe
body: <p>I have timestamped data in a csv with a resolution of 10 minutes, <a href="https://i.stack.imgur.com/3VDkb.png" rel="nofollow noreferrer">as shown in this image</a>. Each timestamp has an "id" which is either 1 or 0. I am trying to create a new column, "time", which returns 0 if "id" = 0, otherwise it should return the value of "time" in the next row plus 10. I am able to do this easily on Excel, and the formula I used is shown in the image.</p>
  
  <p>I am trying to use Python to do the same thing. After importing the csv data as a dataframe using pandas, I had a look at <a href="https://stackoverflow.com/questions/40733560/using-the-values-of-a-previous-row-in-a-pandas-series">this link</a> and <a href="https://stackoverflow.com/questions/323750/how-to-access-previous-next-element-while-for-looping">this link</a>. I copied the "id" column to a new column, "time" and used the following code, but the values of "time" remain exactly the same (1 or 0).</p>
  
  <pre><code>for i, e in enumerate(df['time']):
      if e==1:
          df.at[i,'time']=df.at[i+1,'time']+10
  </code></pre>
  
  <p>Based on the solution in the first link, I tried changing the value of '1' to null and using <code>if pd.isnull(e)</code>, but ended up getting <code>TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'</code>.</p>
  
  <p>I attempted it again by ordering the rows in reverse, changing <code>i+1</code> to <code>i-1</code> but it didn't work either, and I was getting <code>KeyError: -1</code>.</p>
  
  <p>Does anyone have any suggestions on how I can do this in Python?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Apologies, I probably didn't explain my question well. I've figured out how to get the solution I wanted. I realised that the problem was due to the dtypes for "id" and "time" were both <code>object</code>. The <code>KeyError: -1</code> problem was solved by ordering the rows in reverse and assigning a value to "time" in the very first row if it doesn't equal 0. I'm going to ignore any row before the first row with "id" of 0, so I used 999999.</p>
  
  <p><strong>Solution:</strong></p>
  
  <pre><code>#sort timestamp in reverse
  df=df.sort_values(by="timestamp",ascending=False)
  
  #change dtype object to int64
  df['id']=df['id'].astype(np.int64)
  df['time']=df['id']
  
  #assigning value to "time" in first row
  if df.loc[0,'time']==0:
      df.set_value(0,'time',0)
  else:
      df.set_value(0,'time',999999)
  
  #using the previous row's value to evaluate "time"
  for i, e in enumerate(df['time']):
      if e==1:
          df.at[i,'time']=df.at[i-1,'time']+10
  
  #sort timestamp back in ascending order
  df=df.sort_values(by="timestamp")
  </code></pre>