Generate Sample dataframes with constraints
<p>I have a dataframe of records that looks like:</p>

<pre><code>   'Location'  'Rec ID'  'Duration'                         'Rec-X'
0     Houston       126          17    [0.2, 0.34, 0.45, ..., 0.28]
1     Chicago       126        19.3    [0.12, 0.3, 0.41, ..., 0.39]
2      Boston       348        17.3    [0.12, 0.3, 0.41, ..., 0.39]
3     Chicago       138        12.3    [0.12, 0.3, 0.41, ..., 0.39]
4    New York       238        11.3    [0.12, 0.3, 0.41, ..., 0.39]
...
500   Chicago       126        19.3    [0.12, 0.3, 0.41, ..., 0.39]
</code></pre>

<p>And as part of a genetic algorithm process, I want to initialize a population (10) of records. I want each of my subset to contain 10 records, however I want NOT to contain the same 'Rec-ID' two times.</p>

<p>Any idea on how to generate those 10 different dataframes?</p>

<p>Thanks,</p>

<p>you can drop duplicates based on your column from the dataframe and then access the 10 elements</p>

<pre><code>df2 = df.drop_duplicates('Rec ID')
df2.head(10)
</code></pre>

<p><strong>EDIT</strong>
If you want to select randomly 10 unique elements
Then something like this will work</p>

<pre><code>def selectRandomUnique(df) :
    d2 = df.sample(n=3).drop_duplicates('ID')
    while len(d2) != 3 :
        d2 = df.sample(n=3).drop_duplicates('ID')
    return d2    
</code></pre>

<p>In this first you randomly select the rows and then drop any duplicates that may exist.</p>

python dataframe genetic