marimo-learn / _site /polars /04_basic_operations.html
Haleshot's picture
relevant assets and workflow
1cce1df unverified
raw
history blame
27.7 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": {"cell_output": "above", "code_editor_font_size": 14, "default_width": "medium", "theme": "light", "dataframes": "rich"}, "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>04 basic operations</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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20Basic%20operations%20on%20data%0A%20%20%20%20%20%20%20%20_By%20%5BJoram%20Mutenge%5D(https%3A%2F%2Fwww.udemy.com%2Fuser%2Fjoram-mutenge%2F)._%0A%0A%20%20%20%20%20%20%20%20In%20this%20notebook%2C%20you'll%20learn%20how%20to%20perform%20arithmetic%20operations%2C%20comparisons%2C%20and%20conditionals%20on%20a%20Polars%20dataframe.%20We'll%20work%20with%20a%20DataFrame%20that%20tracks%20software%20usage%20by%20year%2C%20categorized%20as%20either%20Vintage%20(old)%20or%20Modern%20(new).%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%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22software%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Lotus-123%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22WordStar%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22dBase%20III%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22VisiCalc%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22WinZip%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22MS-DOS%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22HyperCard%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22WordPerfect%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Excel%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Photoshop%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Visual%20Studio%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Slack%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Zoom%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Notion%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Figma%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Spotify%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22VSCode%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Docker%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22users%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2010000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204500%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202500%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201800%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2017000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201900%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20500000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2012000000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201500000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203000000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204000000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202000000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202500000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204500000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%206000000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203500000%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22category%22%3A%20%5B%22Vintage%22%5D%20*%208%20%2B%20%5B%22Modern%22%5D%20*%2010%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22year%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201985%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201980%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201984%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201979%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201991%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201981%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201987%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201982%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201987%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201990%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201997%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202013%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202011%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202016%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202016%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202008%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202015%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202013%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%0A%20%20%20%20df%0A%20%20%20%20return%20df%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Arithmetic%0A%20%20%20%20%20%20%20%20%23%23%23%20Addition%0A%20%20%20%20%20%20%20%20Let's%20add%2042%20users%20to%20each%20piece%20of%20software.%20This%20means%20adding%2042%20to%20each%20value%20under%20**users**.%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_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22)%20%2B%2042)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Another%20way%20to%20perform%20the%20above%20operation%20is%20using%20the%20built-in%20function.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22).add(42))%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Subtraction%0A%20%20%20%20%20%20%20%20Let's%20subtract%2042%20users%20to%20each%20piece%20of%20software.%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_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22)%20-%2042)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Alternatively%2C%20you%20could%20subtract%20like%20this%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22).sub(42))%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Division%0A%20%20%20%20%20%20%20%20Suppose%20the%20**users**%20values%20are%20inflated%2C%20we%20can%20reduce%20them%20by%20dividing%20by%201000.%20Here's%20how%20to%20do%20it.%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_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22)%20%2F%201000)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Or%20we%20could%20do%20it%20with%20a%20built-in%20expression.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22).truediv(1000))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22If%20we%20didn't%20care%20about%20the%20remainder%20after%20division%20(i.e%20remove%20numbers%20after%20decimal%20point)%20we%20could%20do%20it%20like%20this.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22).floordiv(1000))%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Multiplication%0A%20%20%20%20%20%20%20%20Let's%20pretend%20the%20*user*%20values%20are%20deflated%20and%20increase%20them%20by%20multiplying%20by%20100.%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_(df%2C%20pl)%3A%0A%20%20%20%20(df.with_columns(pl.col(%22users%22)%20*%20100))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Polars%20also%20has%20a%20built-in%20function%20for%20multiplication.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns(pl.col(%22users%22).mul(100))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22So%20far%2C%20we've%20only%20modified%20the%20values%20in%20an%20existing%20column.%20Let's%20create%20a%20column%20**decade**%20that%20will%20represent%20the%20years%20as%20decades.%20Thus%201985%20will%20be%201980%20and%202008%20will%20be%202000.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df.with_columns(decade%3Dpl.col(%22year%22).floordiv(10).mul(10)))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22We%20could%20create%20a%20new%20column%20another%20way%20as%20follows%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20df.with_columns((pl.col(%22year%22).floordiv(10).mul(10)).alias(%22decade%22))%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20**Tip**%20%20%0A%20%20%20%20%20%20%20%20Polars%20encounrages%20you%20to%20perform%20your%20operations%20as%20a%20chain.%20This%20enables%20you%20to%20take%20advantage%20of%20the%20query%20optimizer.%20We'll%20build%20upon%20the%20above%20code%20as%20a%20chain.%0A%0A%20%20%20%20%20%20%20%20%23%23%20Comparison%0A%20%20%20%20%20%20%20%20%23%23%23%20Equal%0A%20%20%20%20%20%20%20%20Let's%20get%20all%20the%20software%20categorized%20as%20Vintage.%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_(df%2C%20pl)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20df.with_columns(decade%3Dpl.col(%22year%22).floordiv(10).mul(10))%0A%20%20%20%20%20%20%20%20.filter(pl.col(%22category%22)%20%3D%3D%20%22Vintage%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(r%22%22%22We%20could%20also%20do%20a%20double%20comparison.%20VisiCal%20is%20the%20only%20software%20that's%20vintage%20and%20in%20the%20decade%201970s.%20Let's%20perform%20this%20comparison%20operation.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20df.with_columns(decade%3Dpl.col(%22year%22).floordiv(10).mul(10))%0A%20%20%20%20%20%20%20%20.filter(pl.col(%22category%22)%20%3D%3D%20%22Vintage%22)%0A%20%20%20%20%20%20%20%20.filter(pl.col(%22decade%22)%20%3D%3D%201970)%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20We%20could%20also%20do%20this%20comparison%20in%20one%20line%2C%20if%20readability%20is%20not%20a%20concern%0A%0A%20%20%20%20%20%20%20%20**Notice**%20that%20we%20must%20enclose%20the%20two%20expressions%20between%20the%20%60%26%60%20with%20parenthesis.%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_(df%2C%20pl)%3A%0A%20%20%20%20(%0A%20%20%20%20%20%20%20%20df.with_columns(decade%3Dpl.col(%22year%22).floordiv(10).mul(10))%0A%20%20%20%20%20%20%20%20.filter((pl.col(%22category%22)%20%3D%3D%20%22Vintage%22)%20%26%20(pl.col(%22decade%22)%20%3D%3D%201970))%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(r%22%22%22We%20can%20also%20use%20the%20built-in%20function%20for%20equal%20to%20comparisons.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(pl.col('category').eq('Vintage'))%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Not%20equal%0A%20%20%20%20%20%20%20%20We%20can%20also%20compare%20if%20something%20is%20%60not%60%20equal%20to%20something.%20In%20this%20case%2C%20category%20is%20not%20vintage.%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_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(pl.col('category')%20!%3D%20'Vintage')%0A%20%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(r%22%22%22Or%20with%20the%20built-in%20function.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(pl.col('category').ne('Vintage'))%0A%20%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(r%22%22%22Or%20if%20you%20want%20to%20be%20extra%20clever%2C%20you%20can%20use%20the%20negation%20symbol%20%60~%60%20used%20in%20logic.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(~pl.col('category').eq('Vintage'))%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Greater%20than%0A%20%20%20%20%20%20%20%20Let's%20get%20the%20software%20where%20the%20year%20is%20greater%20than%202008%20from%20the%20above%20dataframe.%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_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(~pl.col('category').eq('Vintage'))%0A%20%20%20%20%20.filter(pl.col('year')%20%3E%202008)%0A%20%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(r%22%22%22Or%20if%20we%20wanted%20the%20year%202008%20to%20be%20included%2C%20we%20could%20use%20great%20or%20equal%20to.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(~pl.col('category').eq('Vintage'))%0A%20%20%20%20%20.filter(pl.col('year')%20%3E%3D%202008)%0A%20%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(r%22%22%22We%20could%20do%20the%20previous%20two%20operations%20with%20built-in%20functions.%20Here's%20with%20greater%20than.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(~pl.col('category').eq('Vintage'))%0A%20%20%20%20%20.filter(pl.col('year').gt(2008))%0A%20%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(r%22%22%22And%20here's%20with%20greater%20or%20equal%20to%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(~pl.col('category').eq('Vintage'))%0A%20%20%20%20%20.filter(pl.col('year').ge(2008))%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20**Note**%3A%20For%20%22less%20than%22%2C%20and%20%22less%20or%20equal%20to%22%20you%20can%20use%20the%20operators%20%60%3C%60%20or%20%60%3C%3D%60.%20Alternatively%2C%20you%20can%20use%20built-in%20functions%20%60lt%60%20or%20%60le%60%20respectively.%0A%0A%20%20%20%20%20%20%20%20%23%23%23%20Is%20between%0A%20%20%20%20%20%20%20%20Polars%20also%20allows%20us%20to%20filter%20between%20a%20range%20of%20values.%20Let's%20get%20the%20modern%20software%20were%20the%20year%20is%20between%202013%20and%202016.%20This%20is%20inclusive%20on%20both%20ends%20(i.e.%20both%20years%20are%20part%20of%20the%20result).%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_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(pl.col('category').eq('Modern'))%0A%20%20%20%20%20.filter(pl.col('year').is_between(2013%2C%202016))%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%23%20Or%20operator%0A%20%20%20%20%20%20%20%20If%20we%20only%20want%20either%20one%20of%20the%20conditions%20in%20the%20comparison%20to%20be%20met%2C%20we%20could%20use%20%60%7C%60%2C%20which%20is%20the%20%60or%60%20operator.%0A%0A%20%20%20%20%20%20%20%20Let's%20get%20software%20that%20is%20either%20modern%20or%20used%20in%20the%20decade%201980s.%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_(df%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter((pl.col('category')%20%3D%3D%20'Modern')%20%7C%20(pl.col('decade')%20%3D%3D%201980))%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Conditionals%0A%20%20%20%20%20%20%20%20Polars%20also%20allows%20you%20create%20new%20columns%20based%20on%20a%20condition.%20Let's%20create%20a%20column%20*status*%20that%20will%20indicate%20if%20the%20software%20is%20%22discontinued%22%20or%20%22in%20use%22.%0A%0A%20%20%20%20%20%20%20%20Here's%20a%20list%20of%20products%20that%20are%20no%20longer%20in%20use.%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%20discontinued_list%20%3D%20%5B'Lotus-123'%2C%20'WordStar'%2C%20'dBase%20III'%2C%20'VisiCalc'%2C%20'MS-DOS'%2C%20'HyperCard'%5D%0A%20%20%20%20return%20(discontinued_list%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22Here's%20how%20we%20can%20get%20a%20dataframe%20of%20the%20products%20that%20are%20discontinued.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20discontinued_list%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.filter(pl.col('software').is_in(discontinued_list))%0A%20%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(r%22%22%22Now%2C%20let's%20create%20the%20**status**%20column.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20discontinued_list%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.with_columns(pl.when(pl.col('software').is_in(discontinued_list))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.then(pl.lit('Discontinued'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.otherwise(pl.lit('In%20use'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.alias('status')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%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%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20Unique%20counts%0A%20%20%20%20%20%20%20%20Sometimes%20you%20may%20want%20to%20see%20only%20the%20unique%20values%20in%20a%20column.%20Let's%20check%20the%20unique%20decades%20we%20have%20in%20our%20DataFrame.%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_(df%2C%20discontinued_list%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.with_columns(pl.when(pl.col('software').is_in(discontinued_list))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.then(pl.lit('Discontinued'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.otherwise(pl.lit('In%20use'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.alias('status')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20.select('decade').unique()%0A%20%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(r%22%22%22Finally%2C%20let's%20find%20out%20the%20number%20of%20software%20used%20in%20each%20decade.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20discontinued_list%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.with_columns(pl.when(pl.col('software').is_in(discontinued_list))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.then(pl.lit('Discontinued'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.otherwise(pl.lit('In%20use'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.alias('status')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%5B'decade'%5D.value_counts()%0A%20%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(r%22%22%22We%20could%20also%20rewrite%20the%20above%20code%20as%20follows%3A%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20discontinued_list%2C%20pl)%3A%0A%20%20%20%20(df%0A%20%20%20%20%20.with_columns(decade%3Dpl.col('year').floordiv(10).mul(10))%0A%20%20%20%20%20.with_columns(pl.when(pl.col('software').is_in(discontinued_list))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.then(pl.lit('Discontinued'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.otherwise(pl.lit('In%20use'))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.alias('status')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20.select('decade').to_series().value_counts()%0A%20%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(r%22%22%22Hopefully%2C%20we've%20picked%20your%20interest%20to%20try%20out%20Polars%20the%20next%20time%20you%20analyze%20your%20data.%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%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>