title: find out the value of a specific variable in python
body: <p>Let's say I have a data frame with 2 columns, 1st column contains the activities such as work, home, sleep etc. and the 2nd column are the duration of each activity. </p>
  
  <p>while iterating through the rows, I want to find out the duration of the last activity of 'sleep' during the current activity I am in.</p>
  
  <p>Is there an easy way to do that?</p>
  
  <p>my data:</p>
  
  <pre><code>duration = np.random.randint(20, size = 30)
  activities = ['work', 'home', 'sleep', 'home','work', 'sleep','work', 'home','sleep', 'home','work', 'sleep','work', 'home','work', 'sleep','work', 'home','work', 'sleep','work', 'home','work', 'sleep','work', 'home','work', 'home', 'work', 'sleep']
  activity_df = pd.DataFrame({'activities':activities, 'duration':duration})
  </code></pre>