我有一个名为'lattice'的数据框,其属性为'level'
g_lattice=lattice.groupby('level')
如何根据级别随机遍历g_lattice中的组。
答案 0 :(得分:3)
In [22]: df = pd.DataFrame({'A': ['foo', 'bar'] * 3,
'B': rand.randn(6),
'C': rand.randint(0, 20, 6)})
In [23]: groups = list(df.groupby('A'))
In [24]: random.shuffle(groups)
In [25]: for g, grp in groups:
print grp
....:
A B C
0 foo 0.900856 4
2 foo -0.122890 19
4 foo -0.267888 8
A B C
1 bar -0.683728 5
3 bar -0.935769 6
5 bar 0.530355 0