title: How to segregate data on the basis of class label using pandas an Python
body: <p>I have data frame with the class label, now on the basis of class level I want to separate data associated with the different class labels. The code is given bellow: </p>
  
  <pre><code>import pandas as pd
  
  df = [[0.572,0.845,-1.616,-0.827,-0.158,-0.097,0],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,2],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,1],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,2],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,3],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,0],
  [0.572,0.845,-1.616,-0.827,-0.158,-0.097,1]]
  
  df = pd.DataFrame(df, columns=["a","b","c","d","e","f","class_label"])
  
  l = list(set(df["class_label"]))
  
  ls = list(df["class_label"])
  
  for l in l:
      for n,ls in enumerate(ls):
        if l == ls:
              print df[n:n+1]
  </code></pre>
  
  <p>Program terminated with the error given bellow: </p>
  
  <pre><code>       a      b      c      d      e      f  class_label
  0  0.572  0.845 -1.616 -0.827 -0.158 -0.097            0
         a      b      c      d      e      f  class_label
  5  0.572  0.845 -1.616 -0.827 -0.158 -0.097            0
  Traceback (most recent call last):
    File "sample.py", line 19, in &lt;module&gt;
      for n,ls in enumerate(ls):
  TypeError: 'numpy.int64' object is not iterable
  </code></pre>
  
  <p>while the Expected Output should be : </p>
  
  <pre><code>class_1
  0.572,0.845,-1.616,-0.827,-0.158,-0.097,0,
  0.572,0.845,-1.616,-0.827,-0.158,-0.097,0,
  class_2
  0.572,0.845,-1.616, -0.27,-0.158,-0.097,1,
  0.572,0.845,-1.616,-0.827,-0.158,-0.097,1
  class_3
  0.572,0.845,-1.616,-0.827,-0.158,-0.097,2,
  0.572,0.845,-1.16,-0.827,-0.158,-0.097,2,
  class_4
  0.572,0.845,-1.616, -0.27,-0.158,-0.097,3,
  </code></pre>