title: How can I apply clustering by condition in python
body: <p>I have a dataset about 50 000 samples and it contains 2 features where the first is binary and the second is continual.  </p>
  
  <p>I would like to use python for using the clustering method in order to create 2 categories.</p>
  
  <p><strong>PS</strong>: I couldn't specify when the first category is limited exactly to make rules. I need to have a category with most numbers = 0, i.e my goal is that in my data there is a part that I must eliminate it. It has the majority of col_A value are 0 I need to remove them but I don't know where they limited</p>
  
  <p>My data look like:</p>
  
  <pre><code>id        Col_A       Col_B
  1           0           2
  2           0           3
  3           1          25 
  4           0          17
  5           0           9
  6           1          30
  7           1           1
  8           1          12
  9           1           3
  10          0           6
  </code></pre>
  
  <p>I would like to get 2 clusters</p>
  
  <ul>
  <li><p>the first category is about rows that contain small numbers and the majority of Col_A values are 0. </p></li>
  <li><p>and the second category should contain bigger numbers regardless of Col_A values.</p></li>
  </ul>
  
  <p><strong>Category 1 must have:</strong></p>
  
  <pre><code>id        Col_A       Col_B     
  1           0           2
  2           0           3
  5           0           9
  7           1           1
  9           1           3
  10          0           6
  </code></pre>
  
  <p>and <strong>category 2 must have:</strong></p>
  
  <pre><code>id        Col_A       Col_B    
  3           1          25 
  4           0          17
  6           1          30
  8           1          12
  </code></pre>
  
  <p>How can I impose to the clustering algorithm that the first category must have a majority of 0 numbers as values of Col_A</p>
  
  <p>I have used the following code but it doesn't work perfectly.</p>
  
  <pre><code>from sklearn.cluster import KMeans
  import numpy as np
  
  data = np.array([[0, 2], [0, 3], [1, 25],[0, 17], [0, 9], [1, 30], [1, 1],[1, 12], [1, 3], [0, 6]])
  kmeans = KMeans(n_clusters=2, random_state=0).fit(data)
  kmeans.labels_
  
  df = pd.DataFrame(data) 
  df['categories'] = kmeans.labels_
  </code></pre>
  
  <p>I gat some problemes like in case of "18" that must be categorised on category 2. </p>
  
  <pre><code>Col_A      Col_B        Category
  0            2           Cat_1
  0            3           Cat_1
  1           12           Cat_2
  0           17           Cat_2
  0            9           Cat_1
  1           30           Cat_2
  1            1           Cat_1
  1           18           Cat_1
  0            3           Cat_1
  0            6           Cat_1
  </code></pre>
  
  <p>Whene I work on my real data i.e with massive sample I get wrong classification. how Can  I  fix this situation</p>