美文网首页python自学
Python_Pandas_性能提升

Python_Pandas_性能提升

作者: Kaspar433 | 来源:发表于2020-03-19 22:44 被阅读0次

Python_Pandas_性能提升

合理使用numpy及Pandas的一些方法,可以使运算速度成倍提升。本文将介绍一些常用的方法,并进行运算速度对比。

首先读取数据。

import pandas as pd
import numpy as np

data = pd.read_csv("gun_deaths_in_america.csv",header=0)
data.head()
year month intent police sex age race hispanic place education
0 2012 1 Suicide 0 M 34 Asian/Pacific Islander 100 Home 4
1 2012 1 Suicide 0 F 21 White 100 Street 3
2 2012 1 Suicide 0 M 60 White 100 Other specified 4
3 2012 2 Suicide 0 M 64 White 100 Home 4
4 2012 2 Suicide 0 M 31 White 100 Other specified 2

一般的apply()方法

def judge_edu(row):
    if row['education'] > 3:
        return 'high'
    else:
        return row['education']

%timeit data['judge_edu'] = data.apply(judge_edu,axis=1)

out:
1.81 s ± 38.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

使用np.where()

where(condition, [x, y]) ,类似于if...else...,如果满足条件则返回x,否则返回y,可以嵌套。

%timeit data['judge_edu'] = np.where(data['education']>3,'high',data['education'])

out:
58.2 ms ± 5.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

使用np.vectorize()

def judge_edu_2(col):
    if col > 3:
        return 'high'
    else:
        return col
    
vectfunc = np.vectorize(judge_edu_2)
%timeit data['judge_edu'] = vectfunc(data['education'])

out:
52.3 ms ± 5.16 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

多条件np.select()

apply()

def judge_age(row):
    if row['age'] > 60:
        return 'old'
    elif row['age'] > 40:
        return 'mid'
    elif row['age'] > 20:
        return 'young'
    elif row['age'] > 10:
        return 'teen'
    else:
        return 'child'

%timeit data['judge_age'] = data.apply(judge_age,axis=1)

out:
2.26 s ± 72.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

np.where()

%timeit data['judage_age_2'] = np.where(data['age']>60,'old',\
                                        np.where(data['age']>40,'mid',\
                                        np.where(data['age']>20,'young',\
                                        np.where(data['age']>10,'teen','child'))))

out:
17.9 ms ± 2.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

np.select()

np.select(condlist, choicelist, default=0) ,类似Excel中的choose函数。

conditions = [data['age']>60,
             data['age']>40,
             data['age']>20,
             data['age']>10]
choices = ['old','mid','young','teen']

%timeit data['judge_age_3'] = np.select(conditions,choices,default='child')

out:
13.4 ms ± 373 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

总结: 相较于pandas的apply方法,对于这种条件判断的计算,计算速度np.select > np.where > apply。

相关文章

  • Python_Pandas_性能提升

    Python_Pandas_性能提升 合理使用numpy及Pandas的一些方法,可以使运算速度成倍提升。本文将介...

  • Flutter 1.17 中的导航解密和性能提升

    Flutter 1.17 中的导航解密和性能提升Flutter 1.17 中的导航解密和性能提升

  • Metal新特性:大幅度提升iOS端性能

    Metal新特性:大幅度提升iOS端性能Metal新特性:大幅度提升iOS端性能

  • 分布式事务 - 伪代码解读

    随着数据规模不断上涨,数据操作的性能越来越低,为了提升性能,在数据库层面,通常通过分库分表提升性能,提升吞吐量,但...

  • 神经网络:提升深度学习模型的表现

    深度学习性能提升的诀窍 深度学习性能提升的诀窍[转载] 原文: How To Improve Deep Learn...

  • Highcharts 性能提升模块

    Highcharts 性能提升模块(boost.js) 是官方发布的用于提升性能的模块,可以轻松的让 Highch...

  • 智能合约和区块链应用的注意事项

    ①性能的提升&业务模式的改变 区块链实现的不是性能的提升,而是业务模式的改变,同时性能大幅下降。区块链目前所使用的...

  • immutable.js笔记

    介绍 immutable不可变对象,react使用它性能很容易提升很多。react提升性能的关键点就是避免重复渲染...

  • 网站架构

    提升系统性能 扩容 加缓存来提升系统并发能力 使用队列进行流量削峰 异步并发机制提升吞吐量或者接口性能 高并发原则...

  • Recitation 5 RAID

    Background CPU 和 Memory 的性能越来越好 SLED 性能提升不够快 如果 Disk 性能不作...

网友评论

    本文标题:Python_Pandas_性能提升

    本文链接:https://www.haomeiwen.com/subject/rziryhtx.html