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:
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!
