Pandas - random selection within group until threshold reached

Viewed 49

I'm having trouble randomly selecting from groups, so that my selection reaches a certain cumulative threshold (a value of 20) of a variable (FoV) in the dataframe. The df looks like this:

df

Currently, I have tried the following, with no success:

df_sample_grouped = df_sample.groupby('SAMPLE_ID')
FOV_m2 = 0
obid = []
for name, group in df_sample_grouped:
        for row_index, row in df_sample_grouped.iterrows():
            if (FOV_m2 + i[1]['FOV_m2']) <= 20:
                FOV_m2 += i[1]['FOV_m2']
                obid.append(i[1]['SAMPLE_ID'])
        print(obid)

The error message I get is as follows:

[69 rows x 15 columns](
Traceback (most recent call last):

  File "<ipython-input-17-b5c0d6a1c0fa>", line 8, in <module>
    for row_index, row in df_sample_grouped.iterrows():

  File "C:\Users\JH11\Anaconda3\envs\openCV\lib\site-packages\pandas\core\groupby\groupby.py", line 752, in __getattr__
    raise AttributeError(

AttributeError: 'DataFrameGroupBy' object has no attribute 'iterrows'

Any help or suggestions would be much appreciated!

1 Answers

You could do this :

df_sample_grouped = df_sample.groupby('SAMPLE_ID')
FOV_m2 = 0
obid = []
memory = {}
for name, group in df_sample_grouped:
    for row in group.sample(frac=1).iterrows():
        if (FOV_m2 + row[1]['FoV']) <= 20:
            FOV_m2 += row[1]['FoV']
            obid.append(row[1]['Phot_No'])
    
    memory[name] = obid
    obid = []
    FOV_m2 = 0


print(memory)

In the variable memory, you will have a dict with the keys as "SAMPLE_ID" which return list of Phot_No such as the total of FoV from these Phot_No do not go beyond 20.

One Note : .sample(frac=1) is used to shuffle the dataframe to add randomness

Have a nice day,

Gabriel :)

Related