别人没那么重要,我也没那么重要,好好活着,把能做的小事做好,够不到的东西就放弃,承认就好。做一个心情好能睡着的人,你所有事情都会在正轨上。

Python数据分析-数据分组统计

1. 分组统计groupby()函数

  通过使用df.groupby(),可以对数据进行分组统计,语法如下:

DataFrame.groupby(by=None, axis=_NoDefault.no_default, level=None, as_index=True, sort=True, group_keys=True, observed=_NoDefault.no_default, dropna=True)

  groupby操作涉及分割对象、应用函数和组合结果的组合,这可以用于对大量数据进行分组,并对这些组进行计算操作。

参数说明:

  • by:mapping, function, label, pd.Grouper or list of such

  Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align()method). If a list or ndarray of length equal to the selected axis is passed, the values are used as-is to determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key.

  • axis:{0 or ‘index’, 1 or ‘columns’}, default 0

  Split along rows (0) or columns (1). For Series this parameter is unused and defaults to 0.

  • level:int, level name, or sequence of such, default None

  If the axis is a MultiIndex (hierarchical), group by a particular level or levels. Do not specify both by and level.

  • as_index:bool, default True

  Return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output. This argument has no effect on filtrations, such as head(), tail(), nth()and in transformations.

  • sort:bool, default True

  Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group. If False, the groups will appear in the same order as they did in the original DataFrame. This argument has no effect on filtrations, such as head(), tail(), nth() and in transformations.

  • group_keys:bool, default True

  When calling apply and the by argument produces a like-indexed (i.e. a transform) result, add group keys to index to identify pieces. By default group keys are not included when the result’s index (and column) labels match the inputs, and are included otherwise.

  • observed:bool, default False

  This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers.

  • dropna:bool, default True

  If True, and if group keys contain NA values, NA values together with row/column will be dropped. If False, NA values will also be treated as the key in groups.

代码示例:

 1 df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
 2                               'Parrot', 'Parrot'],
 3                    'Max Speed': [380., 370., 24., 26.]})
 4 print(df)
 5 
 6 ### 结果
 7 #    Animal  Max Speed
 8 # 0  Falcon      380.0
 9 # 1  Falcon      370.0
10 # 2  Parrot       24.0
11 # 3  Parrot       26.0
1 df1 = df.groupby(['Animal']).mean()
2 print(df1)
3 
4 ### 结果
5 #         Max Speed
6 # Animal           
7 # Falcon      375.0
8 # Parrot       25.0

  我们可以使用level参数按层次索引的不同级别分组

 1 arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'],
 2           ['Captive', 'Wild', 'Captive', 'Wild']]
 3 index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
 4 df = pd.DataFrame({'Max Speed': [390., 350., 30., 20.]},
 5                   index=index)
 6 print(df)
 7 
 8 ### 结果
 9 #                 Max Speed
10 # Animal Type              
11 # Falcon Captive      390.0
12 #        Wild         350.0
13 # Parrot Captive       30.0
14 #        Wild          20.0
 1 df1 = df.groupby(level=0).mean()
 2 print(df1)
 3 
 4 ### 结果
 5 #         Max Speed
 6 # Animal           
 7 # Falcon      370.0
 8 # Parrot       25.0
 9 
10 df1 = df.groupby(level="Type").mean()
11 print(df1)
12 
13 ### 结果
14 #          Max Speed
15 # Type              
16 # Captive      210.0
17 # Wild         185.0

  我们也可以通过设置dropna参数来选择是否将NA包含在组键中,默认设置为True 

 1 list1 = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]]
 2 df = pd.DataFrame(list1, columns=["a", "b", "c"])
 3 print(df)
 4 
 5 ### 结果
 6 #    a    b  c
 7 # 0  1  2.0  3
 8 # 1  1  NaN  4
 9 # 2  2  1.0  3
10 # 3  1  2.0  2
1 df1 = df.groupby(by=["b"]).sum()
2 print(df1)
3 
4 ### 结果
5 #      a  c
6 # b        
7 # 1.0  2  3
8 # 2.0  2  5
1 df1 = df.groupby(by=["b"], dropna=False).sum()
2 print(df1)
3 
4 ### 结果
5 #      a  c
6 # b        
7 # 1.0  2  3
8 # 2.0  2  5
9 # NaN  1  4

  当使用.apply()时,使用group_keys来包含或排除组键。参数group_keys默认为True(包含)。

 1 df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',
 2                               'Parrot', 'Parrot'],
 3                    'Max Speed': [380., 370., 24., 26.]})
 4 print(df)
 5 
 6 ### 结果
 7 #    Animal  Max Speed
 8 # 0  Falcon      380.0
 9 # 1  Falcon      370.0
10 # 2  Parrot       24.0
11 # 3  Parrot       26.0
 1 df1 = df.groupby("Animal", group_keys=True)[['Max Speed']].apply(lambda x: x)
 2 print(df1)
 3 
 4 ### 结果
 5 #           Max Speed
 6 # Animal             
 7 # Falcon 0      380.0
 8 #        1      370.0
 9 # Parrot 2       24.0
10 #        3       26.0
1 df1 = df.groupby("Animal", group_keys=False)[['Max Speed']].apply(lambda x: x)
2 print(df1)
3 
4 ### 结果
5 #    Max Speed
6 # 0      380.0
7 # 1      370.0
8 # 2       24.0
9 # 3       26.0

 

时间:2024年2月6日

 

posted @ 2024-02-06 10:45  一路狂奔的乌龟  阅读(83)  评论(0)    收藏  举报
返回顶部