Skip to Content
Python数据科学手册
book

Python数据科学手册

by Jake VanderPlas
January 2018
Intermediate to advanced
470 pages
13h 23m
Chinese
Posts & Telecom Press
Content preview from Python数据科学手册
Pandas
数据处理
|
189
np.allclose(result1, result2)
Out[25]: True
3.13.5
 性能决定使用时机
在考虑要不要用这两个函数时,需要思考两个方面:
计算时间
和
内存消耗
,而内存消耗是
更重要的影响因素。就像前面介绍的那样,每个涉及
NumPy
数组或
Pandas
的
DataFrame
的复合代数式都会产生临时数组,例如:
In[26]: x = df[(df.A < 0.5) & (df.B < 0.5)]
它基本等价于:
In[27]: tmp1 = df.A < 0.5
tmp2 = df.B < 0.5
tmp3 = tmp1 & tmp2
x = df[tmp3]
如果临时 DataFrame 的内存需求比你的系统内存还大(通常是几吉字节),那么最好还是使
用 eval() 和 query() 代数式。你可以通过下面的方法大概估算一下变量的内存消耗:
In[28]: df.values.nbytes
Out[28]: 32000
在性能方面,即使你没有使用最大的系统内存,eval() 的计算速度也比普通方法快。现在的
性能瓶颈变成了临时 DataFrame 与系统
CPU
的
L1
和
L2
缓存(在
2016
年依然是几兆字节)
之间的对比了——如果系统缓存足够大,那么 eval() 就可以避免在不同缓存间缓慢地移动
临时文件。在实际工作中,我发现普通的计算方法与 eval
/
query 计算方法在计算时间上的差
异并非总是那么明显,普通方法在处理较小的数组时反而速度更快! ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

流畅的Python

流畅的Python

Luciano Ramalho
Python数据分析基础

Python数据分析基础

Clinton W. Brownley
Python数据处理

Python数据处理

Jacqueline Kazil, Katharine Jarmul

Publisher Resources

ISBN: 9787115475893