df.groupby(
by=None,
axis=0,
level=None,
as_index: bool = True,
sort: bool = True,
group_keys: bool = True,
squeeze: bool = False,
observed: bool = False,
)




字典中的键不需要包含所有的index,也可以包含index中没有的键



作为分组键传递的函数将会按照每个索引值调用一次,同时返回值会被用作分组名称。

pandas按照列groupby后,可以按照值取对应的group。
当对多列进行groupby时,get_group时需要输入多列值的tuple,比如get_group((‘a’, 1)),其中’a’为第一个group列中的某个值,1为第二个group列中的某个值。
import pandas as pd
df = pd.DataFrame({'col1':['a', 'b', 'c','a', 'b', 'c'], 'col2':[1,2,3,4,5,6]})
df
>>> col1 col2
0 a 1
1 b 2
2 c 3
3 a 4
4 b 5
5 c 6
df.groupby('col1').get_group('a')
>>> col1 col2
0 a 1
3 a 4
df.groupby(['col1', 'col2']).get_group(('a', 1))
>>> col1 col2
0 a 1