marimo-learn / _site /polars /01_why_polars.html
Haleshot's picture
Update course content and HTML configurations across various sections, including functional programming, optimization, and probability. Adjust descriptions, enhance user configurations, and ensure consistent theming for improved user experience.
e0f3bfe unverified
raw
history blame
22.9 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="./favicon.ico" />
<!-- Preload is necessary because we show these images when we disconnect from the server,
but at that point we cannot load these images from the server -->
<link rel="preload" href="./assets/gradient-yHQUC_QB.png" as="image" />
<link rel="preload" href="./assets/noise-60BoTA8O.png" as="image" />
<!-- Preload the fonts -->
<link rel="preload" href="./assets/Lora-VariableFont_wght-B2ootaw-.ttf" as="font" crossorigin="anonymous" />
<link rel="preload" href="./assets/PTSans-Regular-CxL0S8W7.ttf" as="font" crossorigin="anonymous" />
<link rel="preload" href="./assets/PTSans-Bold-D9fedIX3.ttf" as="font" crossorigin="anonymous" />
<link rel="preload" href="./assets/FiraMono-Regular-BTCkDNvf.ttf" as="font" crossorigin="anonymous" />
<link rel="preload" href="./assets/FiraMono-Medium-DU3aDxX5.ttf" as="font" crossorigin="anonymous" />
<link rel="preload" href="./assets/FiraMono-Bold-CLVRCuM9.ttf" as="font" crossorigin="anonymous" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="a marimo app" />
<link rel="apple-touch-icon" href="./apple-touch-icon.png" />
<link rel="manifest" href="./manifest.json" />
<script data-marimo="true">
function __resizeIframe(obj) {
var scrollbarHeight = 20; // Max between windows, mac, and linux
function setHeight() {
var element = obj.contentWindow.document.documentElement;
// If there is no vertical scrollbar, we don't need to resize the iframe
if (element.scrollHeight === element.clientHeight) {
return;
}
// Create a new height that includes the scrollbar height if it's visible
var hasHorizontalScrollbar = element.scrollWidth > element.clientWidth;
var newHeight = element.scrollHeight + (hasHorizontalScrollbar ? scrollbarHeight : 0);
// Only update the height if it's different from the current height
if (obj.style.height !== `${newHeight}px`) {
obj.style.height = `${newHeight}px`;
}
}
// Resize the iframe to the height of the content and bottom scrollbar height
setHeight();
// Resize the iframe when the content changes
const resizeObserver = new ResizeObserver((entries) => {
setHeight();
});
resizeObserver.observe(obj.contentWindow.document.body);
}
</script>
<marimo-filename hidden>notebook.py</marimo-filename>
<marimo-mode data-mode='edit' hidden></marimo-mode>
<marimo-version data-version='0.11.9' hidden></marimo-version>
<marimo-user-config data-config='{"completion": {"activate_on_typing": true, "copilot": false}, "display": {"code_editor_font_size": 14, "default_width": "medium", "dataframes": "rich", "theme": "light", "cell_output": "above"}, "formatting": {"line_length": 79}, "keymap": {"preset": "default", "overrides": {}}, "runtime": {"auto_instantiate": true, "auto_reload": "off", "on_cell_change": "autorun", "watcher_on_save": "lazy", "output_max_bytes": 8000000, "std_stream_max_bytes": 1000000}, "save": {"autosave": "off", "autosave_delay": 1000, "format_on_save": false}, "package_management": {"manager": "pip"}, "server": {"browser": "default", "follow_symlink": false}, "snippets": {"custom_paths": [], "include_default_snippets": true}}' data-overrides='{}' hidden></marimo-user-config>
<marimo-app-config data-config='{"width": "medium"}' hidden></marimo-app-config>
<marimo-server-token data-token='123' hidden></marimo-server-token>
<title>01 why polars</title>
<script type="module" crossorigin src="./assets/index-BiV-b1K2.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-DkqMrX_B.css">
<marimo-wasm hidden=""></marimo-wasm>
<script>
if (window.location.protocol === 'file:') {
alert('Warning: This file must be served by an HTTP server to function correctly.');
}
</script>
<style>
#save-button {
display: none !important;
}
#filename-input {
display: none !important;
}
</style>
<marimo-code hidden="" data-show-code="false">import%20marimo%0A%0A__generated_with%20%3D%20%220.11.9%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%20An%20introduction%20to%20Polars%0A%0A%20%20%20%20%20%20%20%20_By%20%5BKoushik%20Khan%5D(https%3A%2F%2Fgithub.com%2Fkoushikkhan)._%0A%0A%20%20%20%20%20%20%20%20This%20notebook%20provides%20a%20birds-eye%20overview%20of%20%5BPolars%5D(https%3A%2F%2Fpola.rs%2F)%2C%20a%20fast%20and%20user-friendly%20data%20manipulation%20library%20for%20Python%2C%20and%20compares%20it%20to%20alternatives%20like%20Pandas%20and%20PySpark.%0A%0A%20%20%20%20%20%20%20%20Like%20Pandas%20and%20PySpark%2C%20the%20central%20data%20structure%20in%20Polars%20is%20**the%20DataFrame**%2C%20a%20tabular%20data%20structure%20consisting%20of%20named%20columns.%20For%20example%2C%20the%20next%20cell%20constructs%20a%20DataFrame%20that%20records%20the%20gender%2C%20age%2C%20and%20height%20in%20centimeters%20for%20a%20number%20of%20individuals.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20df_pl%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%22gender%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20%5B13%2C%2015%2C%2017%2C%2019%2C%2021%2C%2023%2C%2025%2C%2027%2C%2029%2C%2031%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22height_cm%22%3A%20%5B150.0%2C%20170.0%2C%20146.5%2C%20142.0%2C%20155.0%2C%20165.0%2C%20170.8%2C%20130.0%2C%20132.5%2C%20162.0%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20df_pl%0A%20%20%20%20return%20df_pl%2C%20pl%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Unlike%20Python's%20earliest%20DataFrame%20library%20Pandas%2C%20Polars%20was%20designed%20with%20performance%20and%20usability%20in%20mind%20%E2%80%94%20Polars%20can%20scale%20to%20large%20datasets%20with%20ease%20while%20maintaining%20a%20simple%20and%20intuitive%20API.%20%0A%0A%20%20%20%20%20%20%20%20Polars'%20performance%20is%20due%20to%20a%20number%20of%20factors%2C%20including%20its%20implementation%20and%20rust%20and%20its%20ability%20to%20perform%20operations%20in%20a%20parallelized%20and%20vectorized%20manner.%20It%20supports%20a%20wide%20range%20of%20data%20types%2C%20advanced%20query%20optimizations%2C%20and%20seamless%20integration%20with%20other%20Python%20libraries%2C%20making%20it%20a%20versatile%20tool%20for%20data%20scientists%2C%20engineers%2C%20and%20analysts.%20Additionally%2C%20Polars%20provides%20a%20lazy%20API%20for%20deferred%20execution%2C%20allowing%20users%20to%20optimize%20their%20workflows%20by%20chaining%20operations%20and%20executing%20them%20in%20a%20single%20pass.%0A%0A%20%20%20%20%20%20%20%20With%20its%20focus%20on%20speed%2C%20scalability%2C%20and%20ease%20of%20use%2C%20Polars%20is%20quickly%20becoming%20a%20go-to%20choice%20for%20data%20professionals%20looking%20to%20streamline%20their%20data%20processing%20pipelines%20and%20tackle%20large-scale%20data%20challenges.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Choosing%20Polars%20over%20Pandas%0A%0A%20%20%20%20%20%20%20%20In%20this%20section%20we'll%20give%20a%20few%20reasons%20why%20Polars%20is%20a%20better%20choice%20than%20Pandas%2C%20along%20with%20examples.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Intuitive%20syntax%0A%0A%20%20%20%20%20%20%20%20Polars'%20syntax%20is%20similar%20to%20PySpark%20and%20intuitive%20like%20SQL%2C%20making%20heavy%20use%20of%20**method%20chaining**.%20This%20makes%20it%20easy%20for%20data%20professionals%20to%20transition%20to%20Polars%2C%20and%20leads%20to%20an%20API%20that%20is%20more%20concise%20and%20readable%20than%20Pandas.%0A%0A%20%20%20%20%20%20%20%20**Example.**%20In%20the%20next%20few%20cells%2C%20we%20contrast%20the%20code%20to%20perform%20a%20basic%20filter%20and%20aggregation%20of%20data%20with%20Pandas%20to%20the%20code%20required%20to%20accomplish%20the%20same%20task%20with%20%60Polars%60.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20df_pd%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Gender%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Age%22%3A%20%5B13%2C%2015%2C%2017%2C%2019%2C%2021%2C%2023%2C%2025%2C%2027%2C%2029%2C%2031%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Height_CM%22%3A%20%5B150.0%2C%20170.0%2C%20146.5%2C%20142.0%2C%20155.0%2C%20165.0%2C%20170.8%2C%20130.0%2C%20132.5%2C%20162.0%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20query%3A%20average%20height%20of%20male%20and%20female%20after%20the%20age%20of%2015%20years%0A%0A%20%20%20%20%23%20step-1%3A%20filter%0A%20%20%20%20filtered_df_pd%20%3D%20df_pd%5Bdf_pd%5B%22Age%22%5D%20%3E%2015%5D%0A%0A%20%20%20%20%23%20step-2%3A%20groupby%20and%20aggregation%0A%20%20%20%20result_pd%20%3D%20filtered_df_pd.groupby(%22Gender%22)%5B%22Height_CM%22%5D.mean()%0A%20%20%20%20result_pd%0A%20%20%20%20return%20df_pd%2C%20filtered_df_pd%2C%20pd%2C%20result_pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22The%20same%20example%20can%20be%20worked%20out%20in%20Polars%20more%20concisely%2C%20using%20method%20chaining.%20Notice%20how%20the%20Polars%20code%20is%20essentially%20as%20readable%20as%20English.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(pl)%3A%0A%20%20%20%20data_pl%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Gender%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Male%22%2C%20%22Female%22%2C%20%22Male%22%2C%20%22Female%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Age%22%3A%20%5B13%2C%2015%2C%2017%2C%2019%2C%2021%2C%2023%2C%2025%2C%2027%2C%2029%2C%2031%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Height_CM%22%3A%20%5B150.0%2C%20170.0%2C%20146.5%2C%20142.0%2C%20155.0%2C%20165.0%2C%20170.8%2C%20130.0%2C%20132.5%2C%20162.0%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20query%3A%20average%20height%20of%20male%20and%20female%20after%20the%20age%20of%2015%20years%0A%0A%20%20%20%20%23%20filter%2C%20groupby%20and%20aggregation%20using%20method%20chaining%0A%20%20%20%20result_pl%20%3D%20data_pl.filter(pl.col(%22Age%22)%20%3E%2015).group_by(%22Gender%22).agg(pl.mean(%22Height_CM%22))%0A%20%20%20%20result_pl%0A%20%20%20%20return%20data_pl%2C%20result_pl%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Notice%20how%20Polars%20uses%20a%20*method-chaining*%20approach%2C%20similar%20to%20PySpark%2C%20which%20makes%20the%20code%20more%20readable%20and%20expressive%20while%20using%20a%20*single%20line*%20to%20design%20the%20query.%0A%20%20%20%20%20%20%20%20Additionally%2C%20Polars%20supports%20SQL-like%20operations%20*natively*%2C%20that%20allows%20you%20to%20write%20SQL%20queries%20directly%20on%20polars%20dataframe%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(data_pl)%3A%0A%20%20%20%20result%20%3D%20data_pl.sql(%22SELECT%20Gender%2C%20AVG(Height_CM)%20FROM%20self%20WHERE%20Age%20%3E%2015%20GROUP%20BY%20Gender%22)%0A%20%20%20%20result%0A%20%20%20%20return%20(result%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20A%20large%20collection%20of%20built-in%20APIs%0A%0A%20%20%20%20%20%20%20%20Polars%20has%20a%20comprehensive%20API%20that%20enables%20to%20perform%20virtually%20any%20operation%20using%20built-in%20methods.%20In%20contrast%2C%20Pandas%20often%20requires%20more%20complex%20operations%20to%20be%20handled%20using%20the%20%60apply%60%20method%20with%20a%20lambda%20function.%20The%20issue%20with%20%60apply%60%20is%20that%20it%20processes%20rows%20sequentially%2C%20looping%20through%20the%20DataFrame%20one%20row%20at%20a%20time%2C%20which%20can%20be%20inefficient.%20By%20leveraging%20Polars'%20built-in%20methods%2C%20you%20can%20operate%20on%20entire%20columns%20at%20once%2C%20unlocking%20the%20power%20of%20**SIMD%20(Single%20Instruction%2C%20Multiple%20Data)**%20parallelism.%20This%20approach%20not%20only%20simplifies%20your%20code%20but%20also%20significantly%20improves%20performance.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Query%20optimization%20%F0%9F%93%88%0A%0A%20%20%20%20%20%20%20%20A%20key%20factor%20behind%20Polars'%20performance%20lies%20in%20its%20**evaluation%20strategy**.%20While%20Pandas%20defaults%20to%20**eager%20execution**%2C%20executing%20operations%20in%20the%20exact%20order%20they%20are%20written%2C%20Polars%20offers%20both%20**eager%20and%20lazy%20execution**.%20With%20lazy%20execution%2C%20Polars%20employs%20a%20**query%20optimizer**%20that%20analyzes%20all%20required%20operations%20and%20determines%20the%20most%20efficient%20way%20to%20execute%20them.%20This%20optimization%20can%20involve%20reordering%20operations%2C%20eliminating%20redundant%20calculations%2C%20and%20more.%20%0A%0A%20%20%20%20%20%20%20%20For%20example%2C%20consider%20the%20following%20expression%20to%20calculate%20the%20mean%20of%20the%20%60Number1%60%20column%20for%20categories%20%22A%22%20and%20%22B%22%20in%20the%20%60Category%60%20column%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20df%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(by%3D%22Category%22).agg(pl.col(%22Number1%22).mean())%0A%20%20%20%20%20%20%20%20%20%20%20%20.filter(pl.col(%22Category%22).is_in(%5B%22A%22%2C%20%22B%22%5D))%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20If%20executed%20eagerly%2C%20the%20%60groupby%60%20operation%20would%20first%20be%20applied%20to%20the%20entire%20DataFrame%2C%20followed%20by%20filtering%20the%20results%20by%20%60Category%60.%20However%2C%20with%20**lazy%20execution**%2C%20Polars%20can%20optimize%20this%20process%20by%20first%20filtering%20the%20DataFrame%20to%20include%20only%20the%20relevant%20categories%20(%22A%22%20and%20%22B%22)%20and%20then%20performing%20the%20%60groupby%60%20operation%20on%20the%20reduced%20dataset.%20This%20approach%20minimizes%20unnecessary%20computations%20and%20significantly%20improves%20efficiency.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Scalability%20%E2%80%94%20handling%20large%20datasets%20in%20memory%20%E2%AC%86%EF%B8%8F%0A%0A%20%20%20%20%20%20%20%20Pandas%20is%20limited%20by%20its%20single-threaded%20design%20and%20reliance%20on%20Python%2C%20which%20makes%20it%20inefficient%20for%20processing%20large%20datasets.%20Polars%2C%20on%20the%20other%20hand%2C%20is%20built%20in%20Rust%20and%20optimized%20for%20parallel%20processing%2C%20enabling%20it%20to%20handle%20datasets%20that%20are%20orders%20of%20magnitude%20larger.%0A%0A%20%20%20%20%20%20%20%20**Example%3A%20Processing%20a%20Large%20Dataset**%0A%20%20%20%20%20%20%20%20In%20Pandas%2C%20loading%20a%20large%20dataset%20(e.g.%2C%2010GB)%20often%20results%20in%20memory%20errors%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%23%20This%20may%20fail%20with%20large%20datasets%0A%20%20%20%20%20%20%20%20df%20%3D%20pd.read_csv(%22large_dataset.csv%22)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20In%20Polars%2C%20the%20same%20operation%20runs%20quickly%2C%20without%20memory%20pressure%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20df%20%3D%20pl.read_csv(%22large_dataset.csv%22)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20Polars%20also%20supports%20lazy%20evaluation%2C%20which%20allows%20you%20to%20optimize%20your%20workflows%20by%20deferring%20computations%20until%20necessary.%20This%20is%20particularly%20useful%20for%20large%20datasets%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20df%20%3D%20pl.scan_csv(%22large_dataset.csv%22)%20%20%23%20Lazy%20DataFrame%0A%20%20%20%20%20%20%20%20result%20%3D%20df.filter(pl.col(%22A%22)%20%3E%201).groupby(%22A%22).agg(pl.sum(%22B%22)).collect()%20%20%23%20Execute%0A%20%20%20%20%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Compatibility%20with%20other%20machine%20learning%20libraries%20%F0%9F%A4%9D%0A%0A%20%20%20%20%20%20%20%20Polars%20integrates%20seamlessly%20with%20popular%20machine%20learning%20libraries%20like%20Scikit-learn%2C%20PyTorch%2C%20and%20TensorFlow.%20Its%20ability%20to%20handle%20large%20datasets%20efficiently%20makes%20it%20an%20excellent%20choice%20for%20preprocessing%20data%20before%20feeding%20it%20into%20ML%20models.%0A%0A%20%20%20%20%20%20%20%20**Example%3A%20Preprocessing%20Data%20for%20Scikit-learn**%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%0A%20%20%20%20%20%20%20%20%23%20Load%20and%20preprocess%20data%0A%20%20%20%20%20%20%20%20df%20%3D%20pl.read_csv(%22data.csv%22)%0A%20%20%20%20%20%20%20%20X%20%3D%20df.select(%5B%22feature1%22%2C%20%22feature2%22%5D).to_numpy()%0A%20%20%20%20%20%20%20%20y%20%3D%20df.select(%22target%22).to_numpy()%0A%0A%20%20%20%20%20%20%20%20%23%20Train%20a%20model%0A%20%20%20%20%20%20%20%20model%20%3D%20LinearRegression()%0A%20%20%20%20%20%20%20%20model.fit(X%2C%20y)%0A%20%20%20%20%20%20%20%20%60%60%60%0A%0A%20%20%20%20%20%20%20%20Polars%20also%20supports%20conversion%20to%20other%20formats%20like%20NumPy%20arrays%20and%20Pandas%20DataFrames%2C%20ensuring%20compatibility%20with%20virtually%20any%20ML%20library%3A%0A%0A%20%20%20%20%20%20%20%20%60%60%60python%0A%20%20%20%20%20%20%20%20%23%20Convert%20to%20Pandas%20DataFrame%0A%20%20%20%20%20%20%20%20pandas_df%20%3D%20df.to_pandas()%0A%0A%20%20%20%20%20%20%20%20%23%20Convert%20to%20NumPy%20array%0A%20%20%20%20%20%20%20%20numpy_array%20%3D%20df.to_numpy()%0A%20%20%20%20%20%20%20%20%60%60%60%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Easy%20to%20use%2C%20with%20room%20for%20power%20users%0A%0A%20%20%20%20%20%20%20%20Polars%20supports%20advanced%20operations%20like%0A%0A%20%20%20%20%20%20%20%20-%20**date%20handling**%0A%20%20%20%20%20%20%20%20-%20**window%20functions**%0A%20%20%20%20%20%20%20%20-%20**joins**%0A%20%20%20%20%20%20%20%20-%20**nested%20data%20types**%0A%0A%20%20%20%20%20%20%20%20which%20is%20making%20it%20a%20versatile%20tool%20for%20data%20manipulation.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Why%20not%20PySpark%3F%0A%0A%20%20%20%20%20%20%20%20While%20**PySpark**%20is%20versatile%20tool%20that%20has%20transformed%20the%20way%20big%20data%20is%20handled%20and%20processed%20in%20Python%2C%20its%20**complex%20setup%20process**%20can%20be%20intimidating%2C%20especially%20for%20beginners.%20In%20contrast%2C%20**Polars**%20requires%20minimal%20setup%20and%20is%20ready%20to%20use%20right%20out%20of%20the%20box%2C%20making%20it%20more%20accessible%20for%20users%20of%20all%20skill%20levels.%0A%0A%20%20%20%20%20%20%20%20When%20deciding%20between%20the%20two%2C%20**PySpark**%20is%20the%20preferred%20choice%20for%20processing%20large%20datasets%20distributed%20across%20a%20**multi-node%20cluster**.%20However%2C%20for%20computations%20on%20a%20**single-node%20machine**%2C%20**Polars**%20is%20an%20excellent%20alternative.%20Remarkably%2C%20Polars%20is%20capable%20of%20handling%20datasets%20that%20exceed%20the%20size%20of%20the%20available%20RAM%2C%20making%20it%20a%20powerful%20tool%20for%20efficient%20data%20processing%20even%20on%20limited%20hardware.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%94%96%20References%0A%0A%20%20%20%20%20%20%20%20-%20%5BPolars%20official%20website%5D(https%3A%2F%2Fpola.rs%2F)%0A%20%20%20%20%20%20%20%20-%20%5BPolars%20vs.%20Pandas%5D(https%3A%2F%2Fblog.jetbrains.com%2Fpycharm%2F2024%2F07%2Fpolars-vs-pandas%2F)%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A</marimo-code></head>
<body>
<div id="root"></div>
</body>
</html>