【发布时间】:2021-07-15 13:02:29
【问题描述】:
我有一个非常大的DataFrame,看起来像这个例子df:
df =
col1 col2 col3
apple red 2.99
apple red 2.99
apple red 1.99
apple pink 1.99
apple pink 1.99
apple pink 2.99
... .... ...
pear green .99
pear green .99
pear green 1.29
我按这样的 2 列分组:
g = df.groupby(['col1', 'col2'])
现在我想选择 3 个随机组。所以我的预期输出是这样的:
col1 col2 col3
apple red 2.99
apple red 2.99
apple red 1.99
pear green .99
pear green .99
pear green 1.29
lemon yellow .99
lemon yellow .99
lemon yellow 1.99
(让我们假设以上三个组是来自 df 的随机组)。 我怎样才能做到这一点?我使用this。但这对我来说并没有帮助。
【问题讨论】:
-
您只需要 3 个组,还是每组只需要 3 个项目?还是两者兼而有之?