4.9 Pandas中的Dataframe 数据分组(Python)

目录

前言

根据统计研究的需要,将原始数据按照某种标准划分成不同的组别,分组后的的数据称为分组数据。

一、初期数据准备

1. 初期数据定义

# -*- coding: utf-8 -*-
import pandas as pd

data = {
    'name': ['NAME0', 'NAME1', 'NAME2', 'NAME3', 'NAME4', 'NAME5', 'NAME6', 'NAME7', 'NAME8', 'NAME9'],

    'age': [0, 6, 2, 8, 6, 5, 6, 7, 8, 8],

    'weight': [110, 101, 102, 101, 101, 105, 105, 107, 108, 109],

    'is_single_dog': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']
}

indexs = ['index0', 'index1', 'index2', 'index3', 'index4', 'index5', 'index6', 'index7', 'index8', 'index9']

df = pd.DataFrame(data, index=indexs)

print(df)

控制台输出结果:

         name  age  weight is_single_dog
index0  NAME0    0     110           yes
index1  NAME1    6     101           yes
index2  NAME2    2     102            no
index3  NAME3    8     101           yes
index4  NAME4    6     101            no
index5  NAME5    5     105            no
index6  NAME6    6     105            no
index7  NAME7    7     107           yes
index8  NAME8    8     108            no
index9  NAME9    8     109            no

二、Dataframe 数据分组

1. 按一列分组

# 按年龄分组
df = df.groupby('age')

print(df)

控制台输出结果:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000021851069FD0>

注意事项:
分组的结果是无法直接输出的,print()只能看到该结果的数据类型。
可以用循环对分组后的结果进行遍历。

可以用循环对分组后的结果进行遍历:

# 按年龄分组
df = df.groupby('is_single_dog')

print(df)
print()

for key, value in df:
    print(key)
    print(value)
    print()

控制台输出结果:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000024A80A79FD0>

no
         name  age  weight is_single_dog
index2  NAME2    2     102            no
index4  NAME4    6     101            no
index5  NAME5    5     105            no
index6  NAME6    6     105            no
index8  NAME8    8     108            no
index9  NAME9    8     109            no

yes
         name  age  weight is_single_dog
index0  NAME0    0     110           yes
index1  NAME1    6     101           yes
index3  NAME3    8     101           yes
index7  NAME7    7     107           yes

2. 按多列分组

# 按age 和 is_single_dog分组
df = df.groupby(['age', 'is_single_dog'])

print(df)
print()

for key, value in df:
    print(key)
    print(value)
    print()

控制台输出结果:

(0, 'yes')
         name  age  weight is_single_dog
index0  NAME0    0     110           yes

(2, 'no')
         name  age  weight is_single_dog
index2  NAME2    2     102            no

(5, 'no')
         name  age  weight is_single_dog
index5  NAME5    5     105            no

(6, 'no')
         name  age  weight is_single_dog
index4  NAME4    6     101            no
index6  NAME6    6     105            no

(6, 'yes')
         name  age  weight is_single_dog
index1  NAME1    6     101           yes

(7, 'yes')
         name  age  weight is_single_dog
index7  NAME7    7     107           yes

(8, 'no')
         name  age  weight is_single_dog
index8  NAME8    8     108            no
index9  NAME9    8     109            no

(8, 'yes')
         name  age  weight is_single_dog
index3  NAME3    8     101           yes

3. 查看每组的统计数据

对数据表中的数值列进行统计,给出包括count = 计数,mean = 平均数,std = 方差,min = 最小值,25% = 四分位数,50% = 二分位数,75% = 四分之三分位数,max = 最大值的信息。不会对非数值列统计。

① 查看所有列的统计信息
# 按weight分组
df = df.groupby(['weight'])

df1 = df.describe()

print(df1)

控制台输出结果:

         age                                               
       count      mean       std  min   25%  50%   75%  max
weight                                                     
101      3.0  6.666667  1.154701  6.0  6.00  6.0  7.00  8.0
102      1.0  2.000000       NaN  2.0  2.00  2.0  2.00  2.0
105      2.0  5.500000  0.707107  5.0  5.25  5.5  5.75  6.0
107      1.0  7.000000       NaN  7.0  7.00  7.0  7.00  7.0
108      1.0  8.000000       NaN  8.0  8.00  8.0  8.00  8.0
109      1.0  8.000000       NaN  8.0  8.00  8.0  8.00  8.0
110      1.0  0.000000       NaN  0.0  0.00  0.0  0.00  0.0

② 查看指定列的统计信息
# 按weight分组,查看age列的统计信息
df = df.groupby(['weight'])['age']

df1 = df.describe()

控制台输出结果:

        count      mean       std  min   25%  50%   75%  max
weight                                                      
101       3.0  6.666667  1.154701  6.0  6.00  6.0  7.00  8.0
102       1.0  2.000000       NaN  2.0  2.00  2.0  2.00  2.0
105       2.0  5.500000  0.707107  5.0  5.25  5.5  5.75  6.0
107       1.0  7.000000       NaN  7.0  7.00  7.0  7.00  7.0
108       1.0  8.000000       NaN  8.0  8.00  8.0  8.00  8.0
109       1.0  8.000000       NaN  8.0  8.00  8.0  8.00  8.0
110       1.0  0.000000       NaN  0.0  0.00  0.0  0.00  0.0

③ 组内离散列计数

unstack()可以将每列的统计信息垂直排列。

# 按weight分组,查看age列的统计信息
df = df.groupby(['weight'])['age']

df1 = df.describe().unstack()

print(df1)

控制台输出结果:

       weight
count  101       3.000000
       102       1.000000
       105       2.000000
       107       1.000000
       108       1.000000
       109       1.000000
       110       1.000000
mean   101       6.666667
       102       2.000000
       105       5.500000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
std    101       1.154701
       102            NaN
       105       0.707107
       107            NaN
       108            NaN
       109            NaN
       110            NaN
min    101       6.000000
       102       2.000000
       105       5.000000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
25%    101       6.000000
       102       2.000000
       105       5.250000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
50%    101       6.000000
       102       2.000000
       105       5.500000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
75%    101       7.000000
       102       2.000000
       105       5.750000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
max    101       8.000000
       102       2.000000
       105       6.000000
       107       7.000000
       108       8.000000
       109       8.000000
       110       0.000000
dtype: float64

④ 组内数值列和

数据表中的列按值是否连续,可以分为连续值列、离散值列。对于离散值列,可以统计其不重复值的个数。对于连续值列,统计不重复值一般没有意义。统计结果是一个Series对象。

# 按weight分组,求和
df = df.groupby(['weight']).sum()

print(df)

控制台输出结果:

        age
weight     
101      20
102       2
105      11
107       7
108       8
109       8
110       0

⑤ 组内成员个数

每组内,按列统计每组的成员数。每列的统计结果是一样的

# 按weight分组,按列统计每组的成员数
df = df.groupby(['weight']).count()

print(df)

控制台输出结果:

        name  age  is_single_dog
weight                          
101        3    3              3
102        1    1              1
105        2    2              2
107        1    1              1
108        1    1              1
109        1    1              1
110        1    1              1

⑥ 组内数值列均值

每组内,统计所有数值列的均值,非数值列无均值。

# 按weight分组,统计所有数值列的均值
df = df.groupby(['weight']).mean()

print(df)

控制台输出结果:

             age
weight          
101     6.666667
102     2.000000
105     5.500000
107     7.000000
108     8.000000
109     8.000000
110     0.000000

# 按is_single_dog分组,统计weight列的均值
df = df.groupby(['is_single_dog'])['weight'].mean()

print(df)

控制台输出结果:

is_single_dog
no     105.00
yes    104.75
Name: weight, dtype: float64

⑦ 组内数值列最大值

每组内,统计所有数值列的最大值

# 按is_single_dog分组,统计所有列最大值
df = df.groupby(['is_single_dog']).max()

print(df)

控制台输出结果:

                name  age  weight
is_single_dog                    
no             NAME9    8     109
yes            NAME7    8     110

# 按is_single_dog分组,统计weight列最大值
df = df.groupby(['is_single_dog'])['weight'].max()

print(df)

控制台输出结果:

is_single_dog
no     109
yes    110
Name: weight, dtype: int64

⑧ 组内应用函数
# 按is_single_dog分组,统计weight列平均值
df = df.groupby(['is_single_dog'])['weight'].apply(np.mean)

print(df)

控制台输出结果:

is_single_dog
no     105.00
yes    104.75
Name: weight, dtype: float64

⑨ 组内不同列用不同函数
# 按is_single_dog分组,统计weight列平均值,age的标准差
df = df.groupby(['is_single_dog'])

df1 = df.agg({
    'weight': np.mean,
    'age': np.std
})
控制台输出结果:

               weight       age
is_single_dog                  
no             105.00  2.228602
yes            104.75  3.593976

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ibun.song

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值