## This gets n random samples from each group. # Create a sample DataFrame data = {‘group’: [‘A’, ‘A’, ‘B’, ‘B’, ‘C’, ‘C’, ‘D’, ‘D’], ‘value’: [1, 2, 3, 4, 5, 6, 7, 8]} df = pd.DataFrame(data) # Define a custom function to randomly select one value from each group def random_select(x): return x.sample(n=1).iloc[0] # Apply the function to each group in the DataFrame random_values = df.groupby(‘group’).apply(random_select)