返回资讯中心
外部精选
大数据
#数据科学#机器学习

How to Use Marimo for Interactive Data Analysis

Learn how to use Marimo for interactive data analysis with Python, Pandas, and Altair, and turn a reactive notebook into a simple shareable dashboard.

KDnuggetsAbid Ali Awan2026/10/1 · 25 天前7 分钟阅读

以下正文同步自 KDnuggets,版权归原站所有,已转换为易读排版。

python -m marimo edit analysis.py

This opens the Marimo editor in your browser. One thing I really like here is that, unlike Jupyter's .ipynb format, Marimo saves the notebook as a normal .py file.

Now, let's create a slightly more realistic sales dataset that we can use throughout the rest of the tutorial. We will generate data for different products, regions, and quarters, along with units sold, pricing, and revenue.

import altair as alt import numpy as np import pandas as pd import marimo as mo

rng = np.random.default_rng(42)

products = [ ("Laptop", "Tech", 800, 1500), ("Phone", "Tech", 500, 1200), ("Tablet", "Tech", 250, 800), ("Monitor", "Tech", 150, 600), ("Keyboard", "Accessories", 30, 150), ("Mouse", "Accessories", 15, 90), ("Headphones", "Accessories", 50, 400), ("Webcam", "Accessories", 40, 250), ] regions = ["US", "Europe", "Asia"] region_scale = {"US": 1.0, "Europe": 0.75, "Asia": 0.55} quarters = ["Q1", "Q2", "Q3", "Q4"]

rows = [] for _quarter in quarters: for _region in regions: for _name, _category, _lo, _hi in products: units_sold = int( rng.integers(_lo, _hi) * region_scale[_region] * rng.uniform(0.7, 1.3) ) unit_price = round(rng.uniform(_lo, _hi) / 8, 2) rows.append( { "product": _name, "category": _category, "region": _region, "quarter": _quarter, "units_sold": units_sold, "unit_price": unit_price, "revenue": round(units_sold * unit_price, 2), } )

df = pd.DataFrame(rows) df

One nice thing about Marimo is that you do not need any extra code just to inspect the DataFrame. By placing df at the end of the cell, Marimo automatically displays it as an interactive table where you can search, sort, and filter the data.

It works with both Pandas and Polars, so you can use whichever DataFrame library you already prefer.

Next, let us add a dropdown for selecting a region and a slider for setting the minimum sales value:

region = mo.ui.dropdown( options=["All"] + sorted(df["region"].unique().tolist()), value="All", label="Region", )

min_sales = mo.ui.slider( start=0, stop=int(df["units_sold"].max()), value=0, label="Minimum units sold", )

mo.hstack([region, min_sales])

Marimo comes with several built-in UI components, including sliders, dropdowns, checkboxes, date pickers, tables, file uploads, and text inputs.

Now we can connect those controls to our DataFrame:

filtered_df = df[df["units_sold"] >= min_sales.value]

if region.value != "All": filtered_df = filtered_df[ filtered_df["region"] == region.value ]

mo.ui.table(filtered_df)

Try changing the region or moving the slider.

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →