title: Circular dependency in pandas dataframe
body: <p>The following code places bets on coin flip results. You start with £100 and risk 5% on each flip, but because my code calculates bet size based on your starting balance, the bet is always £5.</p>
  
  <pre><code>import pandas
  import matplotlib.pyplot as plt
  
  start_bal = 100.0 #start off with £100
  risk = 0.05 # risk 5% on each bet
  
  #create an empty data frame.
  a = pandas.DataFrame()
  
  #create a list of coin toss results, 1 is win, -1 is lose
  a['Result'] = [1,1,1,1,-1,-1,1,1,1,-1,-1,1,1,-1,-1,-1,1,1]
  
  #your bet size is a % of your starting balance
  a['bet'] = start_bal*risk
  #record profit or loss based on coin toss
  a['pnl'] = a.Result * a.bet
  #increase/decrease balance
  a['bal'] = start_bal + a.pnl.cumsum()
  
  #plot balance
  plt.plot(a.bal)
  </code></pre>
  
  <p>What I would like to do is re-calculate bet size after each bet depending on your balance at that time so you're betting more when your balance increases and less when it decreases. This would mean that 'bal' depends on the 'bet', which in turn depends on 'bal' so I end up with a circular relationship.</p>
  
  <p>Is this possible to do? Would I need to iterate through the dataframe one row at a time, re-calculating the 'bal' and 'bet' at that particular index?</p>
  
  <p>Thanks.</p>