【发布时间】:2018-03-04 15:23:06
【问题描述】:
【问题讨论】:
-
您可以使用
for循环遍历counts
标签: python pandas dataframe pandas-groupby
【问题讨论】:
for 循环遍历counts
标签: python pandas dataframe pandas-groupby
你可以使用get_group方法:
In [21]: gb.get_group('foo')
Out[21]:
A B C
0 foo 1.624345 5
2 foo -0.528172 11
4 foo 0.865408 14
注意:这不需要为每个组创建中间字典/每个子数据帧的副本,因此比使用 dict(iter(gb)) 创建简单字典更节省内存。这是因为它使用了 groupby 对象中已有的数据结构。
您可以使用 groupby 切片选择不同的列:
In [22]: gb[["A", "B"]].get_group("foo")
Out[22]:
A B
0 foo 1.624345
2 foo -0.528172
4 foo 0.865408
In [23]: gb["C"].get_group("foo")
Out[23]:
0 5
2 11
4 14
Name: C, dtype: int64
【讨论】: