0

I have a dataframe of records that looks like:

   '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]

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.

Any idea on how to generate those 10 different dataframes?

Thanks,

4

1 に答える 1

1

データフレームから列に基づいて重複を削除し、10 個の要素にアクセスできます

df2 = df.drop_duplicates('Rec ID')
df2.head(10)

編集 ランダムに10個のユニークな要素を選択したい場合は、このようなものが機能します

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    

この最初の行では、行をランダムに選択してから、存在する可能性のある重複を削除します。

于 2016-10-17T03:04:41.913 に答える