DataFrame column selectionin GroupBy¶
Once you have created the GroupBy object from a DataFrame, youmight want to do something different for each of the columns. Thus, using [] similar to getting a column froma DataFrame, you can do:
一旦你已经用一个DF创造了一个GroupBy对象,你可能想要对每列做不 同的事情,因此使用中括号[]来得到DF中的一列,你可以做
In [53]: grouped =df.groupby(['A'])
用A分组后,得到两列,分别C和D
注意这里的grouped是DataFrameGroupBy
In [54]: grouped_C =grouped['C']
所以说和DF中获取一列是一样的。
In [55]: grouped_D= grouped['D']
但grouped_C和D是SeriesGroupBy
This is mainly syntactic sugar for the alternative and much moreverbose:
这一模一样的效果也可以用以下的语句得到
In [56]: df['C'].groupby(df['A'])
这个也是SeriesGroupBy
Additionally this method avoids recomputing the internalgrouping information derived from the passed key
另外,这个方法避免了重新计算这个内部的分级信息来源于使用过的关键词。
Iterating through groups¶
With the GroupBy object in hand, iterating through the groupeddata is very natural and functions similarly to itertools.groupby():
通过组进行迭代
在唾手可得GroupBy对象,可以对DataFrameGroupBy数据进行迭代是非常自然的,与下面这个函数相似。Itertools.groupby()
grouped = df.groupby('A')
In [58]: for name, group in grouped:
....: print(name)
....: print(group)
print(name)##就只是按照A分组的后分级标志形成列的名字,也就是A分组后的两个特征值bar二是foo
print(group)##可以看到是按照A分组展示的,A分组就只两个特征值一是bar二是foo,分别展示了BCD列
In thecase of grouping by multiple keys, the group name will be a tuple:
在有多个关键词的例子中,组的名字就是一个元组。Will be是事实表意
In [59]: for name, group in df.groupby(['A', 'B']):
....: print(name)
....: print(group)
名字分别是('bar','one')、('bar', 'three')、('bar', 'two')、('foo', 'one')、('foo', 'three')、('foo', 'two'),这只是tuple的key不是value
('bar','one')key
A B C D1 value
bar one2 -2
See Iteratingthrough groups还可以去查看用组迭代。
Selecting a group¶
A single group can be selected using get_group():
选择组,一个单个组可以通过get_group()来选择。
In [60]: grouped.get_group('bar')
Out[60]:
A B C D
1 bar one 0.254161 1.511763
3 bar three 0.215897 -0.990582
5 bar two -0.077118 1.211526
按bar分组,得到这个对象是一个DataFrame
Or for an object grouped on multiple columns:
或者对于多个列对一个对象分组。
df.groupby(['A', 'B']).get_group(('bar', 'one'))
先对AB分组然后对bar one单独构建DF
Out[61]:
A B C D
1 bar one 0.254161 1.511763
按bar分组,得到这个对象是一个DataFrame