利用pandas对数据离散化

在实际的工作场景中,我们经常会遇到这样一种场景:想要将某些字段进行离散化即分桶,简单来说就是讲年龄分成几个区间。pandas中的cut方法能很好地完成此操作。

#导入相关库,并创建数据集
import pandas as pd 
import numpy as np

index = pd.Index(data=["Tom", "Bob", "Mary", "James"], name="name")
data = {
    "age": [15, 28, 23, 37],
    "city": ["Hangzhou", "ShangHai", "Hefei", "Luan"],
    "sex": ["male", "female", "female", "male"]
}
user_info = pd.DataFrame(data=data, index=index)

In [48]:user_info
Out[48]: 
       age      city     sex
name                        
Tom     15  Hangzhou    male
Bob     28  ShangHai  female
Mary    23     Hefei  female
James   37      Luan    male

#将user_info中的age分成三个年龄段
pd.cut(user_info.age,3)

Out[51]: 
name
Tom      (14.978, 22.333]
Bob      (22.333, 29.667]
Mary     (22.333, 29.667]
James      (29.667, 37.0]
Name: age, dtype: category
Categories (3, interval[float64]): [(14.978, 22.333] < (22.333, 29.667] < (29.667, 37.0]]

从以上得到的结果可以看出,cut将年龄段进行了均等的切分。当然我们还可以进行自定义操作(此时可以对区间名称起别名):

#自定义区间并进行分割
qujian=[5,15,25,40]
pd.cut(user_info.age,qujian)

Out[55]: 
name
Tom       (5, 15]
Bob      (25, 40]
Mary     (15, 25]
James    (25, 40]
Name: age, dtype: category
Categories (3, interval[int64]): [(5, 15] < (15, 25] < (25, 40]]

#起别名
pd.cut(user_info.age,qujian,labels=['child','youth','middle'])
Out[56]: 
name
Tom       child
Bob      middle
Mary      youth
James    middle
Name: age, dtype: category
Categories (3, object): [child < youth < middle]

如果现在想求出每个区间出现male和female出现的次数,该如何操作呢?可以结合groupby函数来进行操作,如下:

#首先对user_info进行切割
pdd=pd.cut(user_info['age'],qujian)
user_info['age'].groupby(pdd).count()
Out[66]: 
age
(5, 15]     1
(15, 25]    1
(25, 40]    2
Name: age, dtype: int64

从上可以看出,实现age区间出现次数的统计。

 

你可能感兴趣的:(Python)