楼主: CDA网校
288 0

在Python中处理十亿行数据集(使用Vaex) [推广有奖]

管理员

已卖:189份资源

泰斗

15%

还不是VIP/贵宾

-

威望
3 级
论坛币
162091 个
通用积分
19657.5439
学术水平
353 点
热心指数
365 点
信用等级
328 点
经验
248531 点
帖子
8084
精华
21
在线时间
4620 小时
注册时间
2019-9-13
最后登录
2026-12-4

初级热心勋章

楼主
CDA网校 学生认证  发表于 2026-2-11 11:48:39 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
<!-- markdown css tag --><section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 0 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: PingFangSC-Light;"><figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/f5965e14-bb9a-42c2-8133-636301652250.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">引言</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">处理包含十亿行的大规模数据集,是数据科学与分析领域的一大挑战。传统工具(如Pandas)在中小规模、能放入系统内存的数据集上表现出色,但随着数据集规模扩大,它们会变得缓慢、占用大量随机存取内存(RAM),且经常因内存不足(OOM)错误崩溃。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这就是Vaex的用武之地——Vaex是一款用于核外数据处理的高性能Python库。它能让你高效、省内存地检查、修改、可视化和分析大型表格数据集,即便在普通笔记本电脑上也能轻松实现。</p>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">什么是Vaex?</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex是一款用于惰性、核外Datafr ame的Python库(类似Pandas),专为超出RAM容量的数据集设计。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">核心特点</span><span class="suffix" style="display: none;"></span></h3>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc; font-size: 14px;">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex通过直接操作磁盘上的数据,仅读取所需部分,避免将整个文件加载到内存,从而高效处理大规模数据集。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex采用惰性求值机制,即仅在实际需要结果时才执行计算;它还能通过内存映射,即时打开按列存储(而非按行存储)的柱状数据库,如HDF5、Apache Arrow和Parquet。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">基于优化的C/C++后端,Vaex每秒可对数十亿行数据进行统计计算和操作,即便在普通硬件上也能实现大规模分析的高速运行。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">拥有类似Pandas的应用程序编程接口(API),让熟悉Pandas的用户能轻松过渡,无需陡峭的学习曲线就能利用大数据处理能力。</p>
</section></li></ul>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">Vaex与Dask的对比</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex并非整体与Dask相似,而是与构建在Pandas Datafr ame之上的Dask Datafr ames类似。这意味着Dask继承了Pandas的某些问题,例如在某些场景下需要将数据完全加载到RAM中才能处理——而Vaex则无需如此。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex不会复制Datafr ame,因此能在内存较少的机器上处理更大的Datafr ame。Vaex和Dask均采用惰性处理,但核心区别在于:Vaex仅在需要时计算字段,而Dask需要显式调用compute()函数。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">若要充分利用Vaex的优势,数据需为HDF5或Apache Arrow格式。</p>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">传统工具为何陷入困境</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Pandas等工具在处理前会将整个数据集加载到RAM中。对于超出内存的数据集,这会导致:</p>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc; font-size: 14px;">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">性能缓慢</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">系统崩溃(内存不足错误)</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">交互性受限</p>
</section></li></ul>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">而Vaex从不将整个数据集加载到内存中,而是:</p>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc; font-size: 14px;">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">从磁盘流式读取数据</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">使用虚拟列和惰性求值延迟计算</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">仅在显式需要时才生成结果</p>
</section></li></ul>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这使得即便在普通硬件上,也能对大规模数据集进行分析。</p>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">Vaex的底层工作原理</span><span class="suffix"></span></h2>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">1. 核外执行(Out-of-Core Execution)</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex通过内存映射,根据需要从磁盘读取数据。这让它能够处理远大于RAM容量的数据文件。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">2. 惰性求值(Lazy Evaluation)</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex不会立即执行每个操作,而是构建一个计算图。仅当你请求结果时(例如打印或绘图),才会执行计算。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">3. 虚拟列(Virtual Columns)</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">虚拟列是在数据集上定义的表达式,在计算前不会占用内存。这能节省RAM并加快工作流程。</p>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">Vaex快速入门</span><span class="suffix"></span></h2>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">1. 安装Vaex</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">创建一个干净的虚拟环境:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">conda&nbsp;create&nbsp;-n&nbsp;vaex_demo&nbsp;python=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3.9</span><br>conda&nbsp;activate&nbsp;vaex_demo<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">使用pip安装Vaex:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">pip&nbsp;install&nbsp;vaex-core&nbsp;vaex-hdf5&nbsp;vaex-viz<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">升级Vaex:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">pip&nbsp;install&nbsp;--upgrade&nbsp;vaex<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">安装支持库:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">pip&nbsp;install&nbsp;pandas&nbsp;numpy&nbsp;matplotlib<br></code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">2. 打开大型数据集</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex支持多种处理大型数据集的流行存储格式,可直接处理HDF5、Apache Arrow和Parquet文件——这些格式均经过优化,能实现高效的磁盘访问和快速分析。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">虽然Vaex也能读取CSV文件,但为了提升处理大型数据集时的性能,它需要先将CSV文件转换为更高效的格式。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">打开Parquet文件的方法:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;vaex<br><br>df&nbsp;=&nbsp;vaex.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"your_huge_dataset.parquet"</span>)<br>print(df)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">现在,你可以在不将数据集加载到内存的情况下,检查其结构。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">3. Vaex核心操作</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">数据过滤:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">filtered&nbsp;=&nbsp;df[df.sales&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1000</span>]<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这不会立即计算结果,而是注册过滤器,仅在需要时应用。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">分组与聚合:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">result&nbsp;=&nbsp;df.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"category"</span>,&nbsp;agg=vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"sales"</span>))<br>print(result)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex使用并行算法和最少内存,高效计算聚合结果。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">统计计算:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">mean_price&nbsp;=&nbsp;df[<span class="hljs-string" style="color: #98c379; line-height: 26px;">"price"</span>].mean()<br>print(mean_price)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex通过分块扫描数据集,即时计算统计结果。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">4. 出租车数据集实战演示</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">我们将创建一个包含5000万行的真实出租车数据集,以演示Vaex的功能:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;vaex<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;numpy&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span>&nbsp;np<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;pandas&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span>&nbsp;pd<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span>&nbsp;time<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;设置随机种子以保证可重复性</span><br>np.random.seed(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">42</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Creating&nbsp;50&nbsp;million&nbsp;row&nbsp;dataset..."</span>)<br>n&nbsp;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">50</span>_000_000<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;生成真实的出租车行程数据</span><br>data&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'passenger_count'</span>:&nbsp;np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_distance'</span>:&nbsp;np.random.exponential(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>:&nbsp;np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1.5</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'tip_amount'</span>:&nbsp;np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_amount'</span>:&nbsp;np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">12</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1.8</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'payment_type'</span>:&nbsp;np.random.choice([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'credit'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'cash'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mobile'</span>],&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_hour'</span>:&nbsp;np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">24</span>,&nbsp;n),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_day'</span>:&nbsp;np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">8</span>,&nbsp;n),<br>}<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;创建Vaex&nbsp;Datafr ame</span><br>df_vaex&nbsp;=&nbsp;vaex.from_dict(data)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;导出为HDF5格式(Vaex高效格式)</span><br>df_vaex.export_hdf5(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'taxi_50M.hdf5'</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Created&nbsp;dataset&nbsp;with&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{n:,}</span>&nbsp;rows"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Shape: (50000000, 8)
Created dataset with 50,000,000 rows
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">现在我们得到了一个包含5000万行、8列的数据集。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">5. Vaex与Pandas性能对比</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex内存映射打开大型文件:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>df_vaex&nbsp;=&nbsp;vaex.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'taxi_50M.hdf5'</span>)<br>vaex_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Vaex&nbsp;opened&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{df_vaex.shape[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>]:,}</span>&nbsp;rows&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{vaex_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Memory&nbsp;usage:&nbsp;~0&nbsp;MB&nbsp;(memory-mapped)"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Vaex opened 50,000,000 rows in 0.0199 seconds
Memory usage: ~0 MB (memory-mapped)
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Pandas:加载到内存(请勿尝试用5000万行数据测试!):</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;大多数机器上会失败</span><br>df_pandas&nbsp;=&nbsp;pd.read_hdf(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'taxi_50M.hdf5'</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这会导致内存错误!Vaex几乎能即时打开文件,无论文件大小,因为它不会将数据加载到内存中。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">基础聚合:计算5000万行数据的统计信息:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>stats&nbsp;=&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_fare'</span>:&nbsp;df_vaex.fare_amount.mean(),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_distance'</span>:&nbsp;df_vaex.trip_distance.mean(),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_revenue'</span>:&nbsp;df_vaex.total_amount.sum(),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'max_fare'</span>:&nbsp;df_vaex.fare_amount.max(),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'min_fare'</span>:&nbsp;df_vaex.fare_amount.min(),<br>}<br>agg_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nComputed&nbsp;5&nbsp;aggregations&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{agg_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Mean&nbsp;fare:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{stats[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_fare'</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Mean&nbsp;distance:&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{stats[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_distance'</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>&nbsp;miles"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Total&nbsp;revenue:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{stats[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_revenue'</span>]:,<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Fare&nbsp;range:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{stats[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'min_fare'</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>&nbsp;-&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{stats[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'max_fare'</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Computed 5 aggregations in 0.8771 seconds:
  Mean fare: $15.00
  Mean distance: 3.00 miles
  Total revenue: $1,080,035,827.27
  Fare range: $1.25 - $55.30
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">过滤操作:筛选长途行程:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>long_trips&nbsp;=&nbsp;df_vaex[df_vaex.trip_distance&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>]<br>filter_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nFiltered&nbsp;for&nbsp;trips&nbsp;&gt;&nbsp;10&nbsp;miles&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{filter_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Found:&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(long_trips):,}</span>&nbsp;long&nbsp;trips"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Percentage:&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{(len(long_trips)/len(df_vaex)*<span class="hljs-number" style="color: #d19a66; line-height: 26px;">100</span>):<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>%"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Filtered for trips &gt; 10 miles in 0.0486 seconds
Found: 1,784,122 long trips
Percentage: 3.57%
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">多条件过滤:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>premium_trips&nbsp;=&nbsp;df_vaex[(df_vaex.trip_distance&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">5</span>)&nbsp;&amp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(df_vaex.fare_amount&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">20</span>)&nbsp;&amp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(df_vaex.payment_type&nbsp;==&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'credit'</span>)]<br>multi_filter_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nMultiple&nbsp;condition&nbsp;filter&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{multi_filter_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"&nbsp;&nbsp;Premium&nbsp;trips&nbsp;(&gt;5mi,&nbsp;&gt;$20,&nbsp;credit):&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(premium_trips):,}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Multiple condition filter in 0.0582 seconds
Premium trips (&gt;5mi, &gt;$20, credit): 457,191
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">分组操作:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>by_payment&nbsp;=&nbsp;df_vaex.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'payment_type'</span>,&nbsp;agg={<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_fare'</span>:&nbsp;vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_tip'</span>:&nbsp;vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'tip_amount'</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_trips'</span>:&nbsp;vaex.agg.count(),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_revenue'</span>:&nbsp;vaex.agg.sum(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_amount'</span>)<br>})<br>groupby_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nGroupBy&nbsp;operation&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{groupby_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(by_payment.to_pandas_df())<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">GroupBy operation in 5.6362 seconds
  payment_type  mean_fare  mean_tip  total_trips  total_revenue
0       credit  15.001817  2.000065     16663623   3.599456e+08
1       mobile  15.001200  1.999679     16667691   3.600165e+08
2         cash  14.999397  2.000115     16668686   3.600737e+08
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">更复杂的分组操作:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>by_hour&nbsp;=&nbsp;df_vaex.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_hour'</span>,&nbsp;agg={<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'avg_distance'</span>:&nbsp;vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_distance'</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'avg_fare'</span>:&nbsp;vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_count'</span>:&nbsp;vaex.agg.count()<br>})<br>complex_groupby_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nGroupBy&nbsp;by&nbsp;hour&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{complex_groupby_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(by_hour.to_pandas_df().head(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>))<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">GroupBy by hour in 1.6910 seconds
   pickup_hour  avg_distance   avg_fare  trip_count
0            0      2.998120  14.997462     2083481
1            1      3.000969  14.998814     2084650
2            2      3.003834  15.001777     2081962
3            3      3.001263  14.998196     2081715
4            4      2.998343  14.999593     2083882
5            5      2.997586  15.003988     2083421
6            6      2.999887  15.011615     2083213
7            7      3.000240  14.996892     2085156
8            8      3.002640  15.000326     2082704
9            9      2.999857  14.997857     2082284
</code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">6. Vaex高级特性</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">虚拟列(计算列)允许在不复制数据的情况下添加列:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">df_vaex[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'tip_percentage'</span>]&nbsp;=&nbsp;(df_vaex.tip_amount&nbsp;/&nbsp;df_vaex.fare_amount)&nbsp;*&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">100</span><br>df_vaex[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'is_generous_tipper'</span>]&nbsp;=&nbsp;df_vaex.tip_percentage&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">20</span><br>df_vaex[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'rush_hour'</span>]&nbsp;=&nbsp;(df_vaex.pickup_hour&nbsp;&gt;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>)&nbsp;&amp;&nbsp;(df_vaex.pickup_hour&nbsp;&lt;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">9</span>)&nbsp;|&nbsp;\<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(df_vaex.pickup_hour&nbsp;&gt;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">17</span>)&nbsp;&amp;&nbsp;(df_vaex.pickup_hour&nbsp;&lt;=&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">19</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">这些列即时计算,无内存开销:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"Added&nbsp;3&nbsp;virtual&nbsp;columns&nbsp;with&nbsp;zero&nbsp;memory&nbsp;overhead"</span>)<br>generous_tippers&nbsp;=&nbsp;df_vaex[df_vaex.is_generous_tipper]<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Generous&nbsp;tippers&nbsp;(&gt;20%&nbsp;tip):&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(generous_tippers):,}</span>"</span>)<br><br>rush_hour_trips&nbsp;=&nbsp;df_vaex[df_vaex.rush_hour]<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Rush&nbsp;hour&nbsp;trips:&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(rush_hour_trips):,}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">VIRTUAL COLUMNS
Added 3 virtual columns with zero memory overhead
Generous tippers (&gt;20% tip): 11,997,433
Rush hour trips: 12,498,848
</code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">相关性分析:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">corr&nbsp;=&nbsp;df_vaex.correlation(df_vaex.trip_distance,&nbsp;df_vaex.fare_amount)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Correlation&nbsp;(distance&nbsp;vs&nbsp;fare):&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{corr:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">分位数计算:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">try</span>:<br>&nbsp;&nbsp;&nbsp;&nbsp;percentiles&nbsp;=&nbsp;df_vaex.percentile_approx(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>,&nbsp;[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">25</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">50</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">75</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">90</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">95</span>,&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">99</span>])<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">except</span>&nbsp;AttributeError:<br>&nbsp;&nbsp;&nbsp;&nbsp;percentiles&nbsp;=&nbsp;[<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.25</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.50</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.75</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.90</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.95</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.99</span>),<br>&nbsp;&nbsp;&nbsp;&nbsp;]<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nFare&nbsp;percentiles:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"25th:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"50th&nbsp;(median):&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"75th:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"90th:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"95th:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">4</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"99th:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{percentiles[<span class="hljs-number" style="color: #d19a66; line-height: 26px;">5</span>]:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">标准差计算:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">std_fare&nbsp;=&nbsp;df_vaex.fare_amount.std()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nStandard&nbsp;deviation&nbsp;of&nbsp;fares:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{std_fare:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">其他常用统计信息:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nAdditional&nbsp;statistics:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Mean:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{df_vaex.fare_amount.mean():<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Min:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{df_vaex.fare_amount.min():<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Max:&nbsp;$<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{df_vaex.fare_amount.max():<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.2</span>f}</span>"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Correlation (distance vs fare): -0.0001

Fare percentiles:
  25th: $11.57
  50th (median): $nan
  75th: $nan
  90th: $nan
  95th: $nan
  99th: $nan

Standard deviation of fares: $4.74

Additional statistics:
  Mean: $15.00
  Min: $1.25
  Max: $55.30
</code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">7. 数据导出</span><span class="suffix" style="display: none;"></span></h3>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">导出过滤后的数据:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;"><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;筛选高价值行程</span><br>high_value_trips&nbsp;=&nbsp;df_vaex[df_vaex.total_amount&nbsp;&gt;&nbsp;<span class="hljs-number" style="color: #d19a66; line-height: 26px;">50</span>]<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">导出为多种格式:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">start&nbsp;=&nbsp;time.time()<br>high_value_trips.export_hdf5(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'high_value_trips.hdf5'</span>)<br>export_time&nbsp;=&nbsp;time.time()&nbsp;-&nbsp;start<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Exported&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(high_value_trips):,}</span>&nbsp;rows&nbsp;to&nbsp;HDF5&nbsp;in&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{export_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>s"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">也可导出为CSV、Parquet等格式:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">high_value_trips.export_csv(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'high_value_trips.csv'</span>)<br>high_value_trips.export_parquet(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'high_value_trips.parquet'</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">Exported 13,054 rows to HDF5 in 5.4508s
</code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">8. 性能汇总面板</span><span class="suffix" style="display: none;"></span></h3>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"VAEX&nbsp;PERFORMANCE&nbsp;SUMMARY"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Dataset&nbsp;size:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{n:,}</span>&nbsp;rows"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"File&nbsp;size&nbsp;on&nbsp;disk:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~2.4&nbsp;GB"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"RAM&nbsp;usage:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~0&nbsp;MB&nbsp;(memory-mapped)"</span>)<br>print()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Open&nbsp;time:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{vaex_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Single&nbsp;aggregation:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{agg_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Simple&nbsp;filter:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{filter_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Complex&nbsp;filter:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{multi_filter_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"GroupBy&nbsp;operation:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{groupby_time:<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.4</span>f}</span>&nbsp;seconds"</span>)<br>print()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Throughput:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~<span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{n/groupby_time:,<span class="hljs-number" style="color: #d19a66; line-height: 26px;">.0</span>f}</span>&nbsp;rows/second"</span>)<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">输出结果:</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px;"><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; background: #282c34; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0px; font-size: 12px; -webkit-overflow-scrolling: touch;">VAEX PERFORMANCE SUMMARY
Dataset size:           50,000,000 rows
File size on disk:      ~2.4 GB
RAM usage:              ~0 MB (memory-mapped)

Open time:              0.0199 seconds
Single aggregation:     0.8771 seconds
Simple filter:          0.0486 seconds
Complex filter:         0.0582 seconds
GroupBy operation:      5.6362 seconds

Throughput:             ~8,871,262 rows/second
</code></pre>
<hr data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; height: 1px; padding: 0; border: none; border-top: medium solidid #333; text-align: center; background-image: linear-gradient(to right,rgba(248,57,41,0),rgba(248,57,41,0.75),rgba(248,57,41,0));">
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-family: STHeitiSC-Light; font-size: 18px; font-weight: bolder; display: inline-block; padding-left: 10px; border-left: 5px solid rgb(248,57,41);">总结思考</span><span class="suffix"></span></h2>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">当你处理大于1GB、无法放入RAM的大规模数据集、探索大数据、对百万行数据进行特征工程,或构建数据预处理流水线时,Vaex是理想选择。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">以下场景不建议使用Vaex:</p>
<ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc; font-size: 14px;">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">数据集小于100MB:此时使用Pandas更简单。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">需对多个表格进行复杂连接:使用结构化查询语言(SQL)数据库更佳。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">需使用完整Pandas API:Vaex的兼容性有限。</p>
</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500; font-size: 13px;"><p style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">处理实时流数据:需使用其他更合适的工具。</p>
</section></li></ul>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; color: black; margin: 10px 10px; line-height: 1.75; letter-spacing: 0.2em; font-size: 14px; word-spacing: 0.1em;">Vaex填补了Python数据科学生态系统的一个空白:无需将所有数据加载到内存,就能高效、交互式地处理十亿行数据集。其核外架构、惰性执行模型和优化算法,使其成为即便在笔记本电脑上也能进行大数据探索的强大工具。无论你是在探索海量日志、科学调查数据还是高频时间序列,Vaex都能帮助你在易用性和大数据扩展性之间搭建桥梁。</p>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">推荐学习书籍 《<strong style="font-weight: border; color: rgb(248,57,41);">CDA一级教材</strong>》适合CDA一级考生备考,也适合业务及数据分析岗位的从业者提升自我。完整电子版已上线CDA网校,累计已有10万+在读~ !</span><span class="suffix" style="display: none;"></span></h3>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://file.haoxueai.cn/haoxue/img/4502d41c-fb3a-43a8-bf50-9e14aedde094.png" alt style="max-width: 100%; border-radius: 0px 0px 5px 5px; display: block; margin: 20px auto; width: 85%; height: 100%; ob ject-fit: contain; box-shadow: #84A1A8 0px 10px 15px;"></figure>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 20px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 14px; color: rgb(165,213,93);">免费加入阅读:<a href="https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0" style="text-decoration: none; word-wrap: break-word; font-weight: bold; color: rgb(248,57,41); border-bottom: 1px solid #ff3502; font-family: STHeitiSC-Light;">https://edu.cda.cn/goods/show/3151?targetId=5147&amp;preview=0</a></span><span class="suffix" style="display: none;"></span></h3>
</section>
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python 数据集 Aggregation correlation performance

您需要登录后才可以回帖 登录 | 我要注册

客服
GMT+8, 2026-12-6 00:00