title: Dataframe manipulation - capturing a change in value
body: <p>I currently have a dataframe as below, which shows a change in position, add 1 unit, subtract 1 unit or do nothing (0). </p>
  
  <p><a href="https://i.stack.imgur.com/QxdY5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QxdY5.png" alt="enter image description here"></a></p>
  
  <p>I'm looking to create a second dataframe with the net position, which is either long (1) or flat (0) - assuming a net short (-1) position is not possible. </p>
  
  <p>So the logic is to start with 0, switch to 1 when the first +1 'change in position' occurs (any subsequent +1 is ignored), then only switch back to 0 when a -1 is seen.</p>
  
  <p>Any thoughts on how to do this? The idea is to create df2 as per below</p>
  
  <p><a href="https://i.stack.imgur.com/ar0Pd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ar0Pd.png" alt="enter image description here"></a></p>
  
  <p>df.cumsum() would work if each +1 'change in position' were to count, but I only wish to capture 'long or flat' not the size of any accumulated long position.</p>
  
  <p>Input data frame: </p>
  
  <p><a href="https://i.stack.imgur.com/Jb2O1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jb2O1.png" alt="enter image description here"></a></p>
  
  <p>Output data frame:</p>
  
  <p><a href="https://i.stack.imgur.com/twU83.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/twU83.png" alt="enter image description here"></a></p>