Python: Random selection per group

后端 未结 9 871
面向向阳花
面向向阳花 2020-12-01 05:08

Say that I have a dataframe that looks like:

Name Group_Id
AAA  1
ABC  1
CCC  2
XYZ  2
DEF  3 
YYH  3

How could I randomly select one (or m

相关标签:
9条回答
  • 2020-12-01 05:54

    A very pandas-ish way:

    takesamp = lambda d: d.sample(n)
    df = df.groupby('Group_Id').apply(takesamp)
    
    0 讨论(0)
  • 2020-12-01 05:56

    Using groupby and random.choice in an elegant one liner:

    df.groupby('Group_Id').apply(lambda x :x.iloc[random.choice(range(0,len(x)))])
    
    0 讨论(0)
  • 2020-12-01 05:56

    The solutions offered fail if a group has fewer samples than the desired sample size n. This addresses this problem:

    n = 10
    df.groupby('Group_Id').apply(lambda x: x.sample(min(n,len(x)))).reset_index(drop=True)
    
    0 讨论(0)
提交回复
热议问题