Python编程 昨天
    以下文章来源于数据分析1480 ,作者刘顺祥
    超全的 100 个 Pandas 函数 - 图1

    来自公众号:数据分析1480

    分享我认为比较常规的100个实用函数,这些函数大致可以分为六类,分别是统计汇总函数、数据清洗函数、数据筛选、绘图与元素级运算函数、时间序列函数和其他函数。


    统计汇总函数

    数据分析过程中,必然要做一些数据的统计汇总工作,那么对于这一块的数据运算有哪些可用的函数可以帮助到我们呢?具体看如下几张表。

    超全的 100 个 Pandas 函数 - 图2

    1. import pandas as pd
    2. import numpy as np
    3. x = pd.Series(np.random.normal(2,3,1000))
    4. y = 3*x + 10 + pd.Series(np.random.normal(1,2,1000))
    5. # 计算x与y的相关系数
    6. print(x.corr(y))
    7. # 计算y的偏度
    8. print(y.skew())
    9. # 计算y的统计描述值
    10. print(x.describe())
    11. z = pd.Series(['A','B','C']).sample(n = 1000, replace = True)
    12. # 重新修改z的行索引
    13. z.index = range(1000)
    14. # 按照z分组,统计y的组内平均值
    15. y.groupby(by = z).aggregate(np.mean)

    超全的 100 个 Pandas 函数 - 图3

    超全的 100 个 Pandas 函数 - 图4

    1. # 统计z中个元素的频次
    2. print(z.value_counts())
    3. a = pd.Series([1,5,10,15,25,30])
    4. # 计算a中各元素的累计百分比
    5. print(a.cumsum() / a.cumsum()[a.size - 1])

    超全的 100 个 Pandas 函数 - 图5


    数据清洗函数

    同样,数据清洗工作也是必不可少的工作,在如下表格中罗列了常有的数据清洗的函数。

    1. x = pd.Series([10,13,np.nan,17,28,19,33,np.nan,27])
    2. #检验序列中是否存在缺失值
    3. print(x.hasnans)
    4. # 将缺失值填充为平均值
    5. print(x.fillna(value = x.mean()))
    6. # 前向填充缺失值
    7. print(x.ffill())

    超全的 100 个 Pandas 函数 - 图6

    超全的 100 个 Pandas 函数 - 图7

    1. income = pd.Series(['12500元','8000元','8500元','15000元','9000元'])
    2. # 将收入转换为整型
    3. print(income.str[:-1].astype(int))
    4. gender = pd.Series(['男','女','女','女','男','女'])
    5. # 性别因子化处理
    6. print(gender.factorize())
    7. house = pd.Series(['大宁金茂府 | 3室2厅 | 158.32平米 | 南 | 精装',
    8. '昌里花园 | 2室2厅 | 104.73平米 | 南 | 精装',
    9. '纺大小区 | 3室1厅 | 68.38平米 | 南 | 简装'])
    10. # 取出二手房的面积,并转换为浮点型
    11. house.str.split('|').str[2].str.strip().str[:-2].astype(float)

    超全的 100 个 Pandas 函数 - 图8


    数据筛选

    数据分析中如需对变量中的数值做子集筛选时,可以巧妙的使用下表中的几个函数,其中部分函数既可以使用在序列身上,也基本可以使用在数据框对象中。
    超全的 100 个 Pandas 函数 - 图9

    1. np.random.seed(1234)
    2. x = pd.Series(np.random.randint(10,20,10))
    3. # 筛选出16以上的元素
    4. print(x.loc[x > 16])
    5. print(x.compress(x > 16))
    6. # 筛选出13~16之间的元素
    7. print(x[x.between(13,16)])
    8. # 取出最大的三个元素
    9. print(x.nlargest(3))
    10. y = pd.Series(['ID:1 name:张三 age:24 income:13500',
    11. 'ID:2 name:李四 age:27 income:25000',
    12. 'ID:3 name:王二 age:21 income:8000'])
    13. # 取出年龄,并转换为整数
    14. print(y.str.findall('age:(\d+)').str[0].astype(int))

    超全的 100 个 Pandas 函数 - 图10


    绘图与元素级函数

    超全的 100 个 Pandas 函数 - 图11

    1. np.random.seed(123)
    2. import matplotlib.pyplot as plt
    3. x = pd.Series(np.random.normal(10,3,1000))
    4. # 绘制x直方图
    5. x.hist()
    6. # 显示图形
    7. plt.show()
    8. # 绘制x的箱线图
    9. x.plot(kind='box')
    10. plt.show()
    11. installs = pd.Series(['1280万','6.7亿','2488万','1892万','9877','9877万','1.2亿'])
    12. # 将安装量统一更改为“万”的单位
    13. def transform(x):
    14. if x.find('亿') != -1:
    15. res = float(x[:-1])*10000
    16. elif x.find('万') != -1:
    17. res = float(x[:-1])
    18. else:
    19. res = float(x)/10000
    20. return res
    21. installs.apply(transform)

    超全的 100 个 Pandas 函数 - 图12

    超全的 100 个 Pandas 函数 - 图13
    超全的 100 个 Pandas 函数 - 图14


    时间序列函数

    超全的 100 个 Pandas 函数 - 图15
    超全的 100 个 Pandas 函数 - 图16
    超全的 100 个 Pandas 函数 - 图17


    其他函数

    超全的 100 个 Pandas 函数 - 图18

    1. import numpy as np
    2. import pandas as pd
    3. np.random.seed(112)
    4. x = pd.Series(np.random.randint(8,18,6))
    5. print(x)
    6. # 对x中的元素做一阶差分
    7. print(x.diff())
    8. # 对x中的元素做降序处理
    9. print(x.sort_values(ascending = False))
    10. y = pd.Series(np.random.randint(8,16,100))
    11. # 将y中的元素做排重处理,并转换为列表对象
    12. y.unique().tolist()

    超全的 100 个 Pandas 函数 - 图19

    超全的 100 个 Pandas 函数 - 图20