title: I want to count number of observations within each subject in PANDAS dataframe
body: <p>I am quite new to using PANDAS and python in general.</p>
  
  <p>I have a hierarchical data set with several subjects, each of whom have some number of observations. The total df is about half a million rows.</p>
  
  <p>I want to calculate the observations number...</p>
  
  <pre><code>## toy problem
  
  d = {'one' : Series(['a', 'a', 'a', 'b', 'b', 'b'], index = [0,1,2,3,4,5]),
       'two' : Series([1.1, 2.5, 3.3, 2.5, 3.3, 9.5], index = [0,1,2,3,4,5])}
  df = DataFrame(d)
  
  for i in df.one.unique():
      for j in range(0,len(df[df.one == i])):
          print j
  </code></pre>
  
  <p>So I want to assign j to a column for each row. I have no problem calculating j but I cannot figure out how to assign it. I have tried using iloc which is incredibly slow, or writing to a list and then joining this to the df, also really slow (currently running for over 30 mins and counting...). I understand that python is best with vectorised problems but I cannot think of a vector solution for this case.</p>
  
  <p>What is the best way to do this? It is really easy and quick in R. I am currently migrating to Python &amp; PANDAS under the expectation that it is faster but this doesnt appear to be the case here.</p>
  
  <p>Any advice please?</p>