<!-- 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 create -n vaex_demo python=<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3.9</span><br>conda activate 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 install vaex-core vaex-hdf5 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 install --upgrade 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 install pandas numpy 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> vaex<br><br>df = 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 = df[df.sales > <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 = df.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">"category"</span>, 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 = 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> vaex<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> numpy <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> np<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> pandas <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">as</span> pd<br><span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">import</span> time<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 设置随机种子以保证可重复性</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 50 million row dataset..."</span>)<br>n = <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;"># 生成真实的出租车行程数据</span><br>data = {<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'passenger_count'</span>: np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_distance'</span>: np.random.exponential(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">3</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>: np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1.5</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'tip_amount'</span>: np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">2</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_amount'</span>: np.random.gamma(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">12</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">1.8</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'payment_type'</span>: np.random.choice([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'credit'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'cash'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'mobile'</span>], n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_hour'</span>: np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">24</span>, n),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_day'</span>: np.random.randint(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">1</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">8</span>, n),<br>}<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 创建Vaex Datafr ame</span><br>df_vaex = vaex.from_dict(data)<br><br><span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 导出为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 dataset with <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{n:,}</span> 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 = time.time()<br>df_vaex = vaex.open(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'taxi_50M.hdf5'</span>)<br>vaex_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Vaex opened <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> rows in <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Memory usage: ~0 MB (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;"># 大多数机器上会失败</span><br>df_pandas = 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 = time.time()<br>stats = {<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_fare'</span>: df_vaex.fare_amount.mean(),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_distance'</span>: df_vaex.trip_distance.mean(),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_revenue'</span>: df_vaex.total_amount.sum(),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'max_fare'</span>: df_vaex.fare_amount.max(),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'min_fare'</span>: df_vaex.fare_amount.min(),<br>}<br>agg_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nComputed 5 aggregations in <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> seconds:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f" Mean fare: $<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" Mean distance: <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> miles"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f" Total revenue: $<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" Fare range: $<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> - $<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 = time.time()<br>long_trips = df_vaex[df_vaex.trip_distance > <span class="hljs-number" style="color: #d19a66; line-height: 26px;">10</span>]<br>filter_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nFiltered for trips > 10 miles in <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f" Found: <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(long_trips):,}</span> long trips"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f" Percentage: <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 > 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 = time.time()<br>premium_trips = df_vaex[(df_vaex.trip_distance > <span class="hljs-number" style="color: #d19a66; line-height: 26px;">5</span>) & <br> (df_vaex.fare_amount > <span class="hljs-number" style="color: #d19a66; line-height: 26px;">20</span>) & <br> (df_vaex.payment_type == <span class="hljs-string" style="color: #98c379; line-height: 26px;">'credit'</span>)]<br>multi_filter_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nMultiple condition filter in <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f" Premium trips (>5mi, >$20, credit): <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 (>5mi, >$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 = time.time()<br>by_payment = df_vaex.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'payment_type'</span>, agg={<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_fare'</span>: vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'mean_tip'</span>: vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'tip_amount'</span>),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_trips'</span>: vaex.agg.count(),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_revenue'</span>: vaex.agg.sum(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'total_amount'</span>)<br>})<br>groupby_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nGroupBy operation in <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> 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 = time.time()<br>by_hour = df_vaex.groupby(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pickup_hour'</span>, agg={<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'avg_distance'</span>: vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_distance'</span>),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'avg_fare'</span>: vaex.agg.mean(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>),<br> <span class="hljs-string" style="color: #98c379; line-height: 26px;">'trip_count'</span>: vaex.agg.count()<br>})<br>complex_groupby_time = time.time() - start<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nGroupBy by hour in <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> 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>] = (df_vaex.tip_amount / df_vaex.fare_amount) * <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>] = df_vaex.tip_percentage > <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>] = (df_vaex.pickup_hour >= <span class="hljs-number" style="color: #d19a66; line-height: 26px;">7</span>) & (df_vaex.pickup_hour <= <span class="hljs-number" style="color: #d19a66; line-height: 26px;">9</span>) | \<br> (df_vaex.pickup_hour >= <span class="hljs-number" style="color: #d19a66; line-height: 26px;">17</span>) & (df_vaex.pickup_hour <= <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 3 virtual columns with zero memory overhead"</span>)<br>generous_tippers = df_vaex[df_vaex.is_generous_tipper]<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Generous tippers (>20% tip): <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(generous_tippers):,}</span>"</span>)<br><br>rush_hour_trips = df_vaex[df_vaex.rush_hour]<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Rush hour trips: <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 (>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 = df_vaex.correlation(df_vaex.trip_distance, df_vaex.fare_amount)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Correlation (distance vs fare): <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> percentiles = df_vaex.percentile_approx(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'fare_amount'</span>, [<span class="hljs-number" style="color: #d19a66; line-height: 26px;">25</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">50</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">75</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">90</span>, <span class="hljs-number" style="color: #d19a66; line-height: 26px;">95</span>, <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> AttributeError:<br> percentiles = [<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.25</span>),<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.50</span>),<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.75</span>),<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.90</span>),<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.95</span>),<br> df_vaex.fare_amount.quantile(<span class="hljs-number" style="color: #d19a66; line-height: 26px;">0.99</span>),<br> ]<br><br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nFare percentiles:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"25th: $<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 (median): $<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: $<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: $<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: $<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: $<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 = df_vaex.fare_amount.std()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"\nStandard deviation of fares: $<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 statistics:"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Mean: $<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: $<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: $<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;"># 筛选高价值行程</span><br>high_value_trips = df_vaex[df_vaex.total_amount > <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 = 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 = time.time() - start<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Exported <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{len(high_value_trips):,}</span> rows to HDF5 in <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 PERFORMANCE SUMMARY"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Dataset size: <span class="hljs-subst" style="color: #e06c75; line-height: 26px;">{n:,}</span> rows"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"File size on disk: ~2.4 GB"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"RAM usage: ~0 MB (memory-mapped)"</span>)<br>print()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Open time: <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Single aggregation: <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Simple filter: <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Complex filter: <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> seconds"</span>)<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"GroupBy operation: <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> seconds"</span>)<br>print()<br>print(<span class="hljs-string" style="color: #98c379; line-height: 26px;">f"Throughput: ~<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> 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&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&preview=0</a></span><span class="suffix" style="display: none;"></span></h3>
</section>


雷达卡





京公网安备 11010802022788号







