I am trying to select 5 review ids for each business from a dataframe with reviews ids and business ids. I thought it would iterate over the groupby object, but this does not work.
df_b = df.groupby('business_id')
selected = random.sample(df_b,5).review_ids
Not sure a DataFrameGroupBy
object is what you want. I think you'd be better off using DataFrame.sample()
for each id.
frames = list()
for buisness_id in df['business_id'].unique():
frame = df[df['business_id'] == business_id].sample(5)
frames.append(frame)
df = pd.concat(frames)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.