title: Python loop to get the first n value's average and variance for panel data
body: <p>This is my first question. I am hoping to get an idea of an efficient python loop code to achieve something different from moving average and moving variance; but the average and variance from the first value to the (n-1)<sup>th</sup>, my data structure is the follows:</p>
  
  <p>First column is id, there are many repeated ids, one id has multiple values with a sequence (second column, 1,2,...,n) and value (third column). Each id may have different maximum sequence (n).</p>
  
  <p>What I want to achieve, is to have two more columns, one is average, one is variance. For each id, I want to calculate the average value of the first n-1's "values" (first n in terms of sequence). And repeat that for variance.</p>
  
  <p>Anyone to help? </p>
  
  <p>I don't know how to post the data here, if anyone could tell me it will be great. Copy/paste from excel does not work.</p>