title: groupby and return all rows of first n groups
body: <p>I have a pandas dataframe like below</p>
  
  <pre><code>&gt;&gt;&gt; df.head()
         0      1       2          3   4           5                      6
  0  35000  26009  OPTIDX  BANKNIFTY  XX  1499351400  BANKNIFTY1770621000CE
  1  35001  26009  OPTIDX  BANKNIFTY  XX  1499351400  BANKNIFTY1770621000PE
  2  35002  26000  OPTIDX      NIFTY  XX  1609425000      NIFTY20DEC10400CE
  3  35003  26000  OPTIDX      NIFTY  XX  1609425000      NIFTY20DEC10400PE
  4  35004  26009  OPTIDX  BANKNIFTY  XX  1499956200  BANKNIFTY1771321100CE
  </code></pre>
  
  <p>I want to group these in by column 5 in sorted order and return first n groups where n can be given as variable.</p>
  
  <p>I did <code>df.sort_values(5).groupby([5])</code> i get <code>&lt;pandas.core.groupby.DataFrameGroupBy object at 0x2afc8d0&gt;</code> </p>
  
  <p>How do I get all rows in first 2 groups. In the sample df above group 1 would be 1499351400, group 2 would be 1499351400 group 3 would be 1609425000</p>
  
  <p>Expected output: when groups required =2 </p>
  
  <pre><code>       0      1       2          3   4           5                      6
  0  35000  26009  OPTIDX  BANKNIFTY  XX  1499351400  BANKNIFTY1770621000CE
  1  35001  26009  OPTIDX  BANKNIFTY  XX  1499351400  BANKNIFTY1770621000PE
  4  35004  26009  OPTIDX  BANKNIFTY  XX  1499956200  BANKNIFTY1771321100CE
  </code></pre>
  
  <p>Update1:After trying @jezrael's</p>
  
  <pre><code>&gt;&gt;&gt; k2=k1[k1.groupby(5).ngroup() &lt; 2]
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/opt/python/2.7/lib/python2.7/site-packages/pandas/core/groupby.py", line 529, in __getattr__
      (type(self).__name__, attr))
  AttributeError: 'DataFrameGroupBy' object has no attribute 'ngroup'
  </code></pre>
  
  <p>Additional: Is it possible to do it without pandas (only python), I may not always find machines with pandas on them. Thanks</p>