DataFrames - marimo (original) (raw)
Interactive dataframes¶
marimo makes you more productive when working with dataframes.
- Display dataframes in a rich, interactive table and chart views
- Transform dataframes with filters, groupbys, aggregations, and more, no code required
- Select data from tables or charts and get selections back in Python as dataframes
marimo integrates with Pandas andPolars dataframes natively.
For a video overview on interactive dataframes, check out our YouTube tutorial.
Displaying dataframes¶
marimo lets you page through, search, sort, and filter dataframes, making it extremely easy to get a feel for your data.
marimo brings dataframes to life.
Display dataframes by including them in the last expression of the cell, just like any other object.
pandaspolarslive example
[](#%5F%5Fcodelineno-0-1)import pandas as pd [](#%5F%5Fcodelineno-0-2) [](#%5F%5Fcodelineno-0-3)df = pd.read_json( [](#%5F%5Fcodelineno-0-4) "https://raw.githubusercontent.com/vega/vega-datasets/master/data/cars.json" [](#%5F%5Fcodelineno-0-5)) [](#%5F%5Fcodelineno-0-6)df
[](#%5F%5Fcodelineno-1-1)import polars as pl [](#%5F%5Fcodelineno-1-2) [](#%5F%5Fcodelineno-1-3)df = pl.read_json( [](#%5F%5Fcodelineno-1-4)"https://raw.githubusercontent.com/vega/vega-datasets/master/data/cars.json" [](#%5F%5Fcodelineno-1-5)) [](#%5F%5Fcodelineno-1-6)df
To opt out of the rich dataframe viewer, use mo.plain:
pandaspolarslive example
[](#%5F%5Fcodelineno-3-1)import pandas as pd [](#%5F%5Fcodelineno-3-2)import marimo as mo [](#%5F%5Fcodelineno-3-3) [](#%5F%5Fcodelineno-3-4)df = pd.read_json( [](#%5F%5Fcodelineno-3-5)"https://raw.githubusercontent.com/vega/vega-datasets/master/data/cars.json" [](#%5F%5Fcodelineno-3-6)) [](#%5F%5Fcodelineno-3-7)mo.plain(df)
[](#%5F%5Fcodelineno-4-1)import polars as pl [](#%5F%5Fcodelineno-4-2)import marimo as mo [](#%5F%5Fcodelineno-4-3) [](#%5F%5Fcodelineno-4-4)df = pl.read_json( [](#%5F%5Fcodelineno-4-5)"https://raw.githubusercontent.com/vega/vega-datasets/master/data/cars.json" [](#%5F%5Fcodelineno-4-6)) [](#%5F%5Fcodelineno-4-7)mo.plain(df)
Transforming dataframes¶
No-code transformations¶
Use mo.ui.dataframe to interactively transform a dataframe with a GUI, no coding required. When you're done, you can copy the code that the GUI generated for you and paste it into your notebook.
Build transformations using a GUI
The transformations you apply will turn into code which is accessible via the "code" tab.
Copy the code of the transformation
pandaspolarslive example
[](#%5F%5Fcodelineno-6-1)# Cell 1 [](#%5F%5Fcodelineno-6-2)import marimo as mo [](#%5F%5Fcodelineno-6-3)import pandas as pd [](#%5F%5Fcodelineno-6-4) [](#%5F%5Fcodelineno-6-5)df = pd.DataFrame({"person": ["Alice", "Bob", "Charlie"], "age": [20, 30, 40]}) [](#%5F%5Fcodelineno-6-6)transformed_df = mo.ui.dataframe(df) [](#%5F%5Fcodelineno-6-7)transformed_df
[](#%5F%5Fcodelineno-7-1)# Cell 2 [](#%5F%5Fcodelineno-7-2)# transformed_df.value holds the transformed dataframe [](#%5F%5Fcodelineno-7-3)transformed_df.value
[](#%5F%5Fcodelineno-8-1)# Cell 1 [](#%5F%5Fcodelineno-8-2)import marimo as mo [](#%5F%5Fcodelineno-8-3)import polars as pl [](#%5F%5Fcodelineno-8-4) [](#%5F%5Fcodelineno-8-5)df = pl.DataFrame({"person": ["Alice", "Bob", "Charlie"], "age": [20, 30, 40]}) [](#%5F%5Fcodelineno-8-6)transformed_df = mo.ui.dataframe(df) [](#%5F%5Fcodelineno-8-7)transformed_df
[](#%5F%5Fcodelineno-9-1)# Cell 2 [](#%5F%5Fcodelineno-9-2)# transformed_df.value holds the transformed dataframe [](#%5F%5Fcodelineno-9-3)transformed_df.value
Custom filters¶
Create custom filters with marimo UI elements, like sliders and dropdowns.
pandaspolarslive example
[](#%5F%5Fcodelineno-11-1)# Cell 1 - create a dataframe [](#%5F%5Fcodelineno-11-2)df = pd.DataFrame({"person": ["Alice", "Bob", "Charlie"], "age": [20, 30, 40]})
[](#%5F%5Fcodelineno-12-1)# Cell 2 - create a filter [](#%5F%5Fcodelineno-12-2)age_filter = mo.ui.slider(start=0, stop=100, value=50, label="Max age") [](#%5F%5Fcodelineno-12-3)age_filter
[](#%5F%5Fcodelineno-13-1)# Cell 3 - display the transformed dataframe [](#%5F%5Fcodelineno-13-2)filtered_df = df[df["age"] < age_filter.value] [](#%5F%5Fcodelineno-13-3)mo.ui.table(filtered_df)
[](#%5F%5Fcodelineno-14-1)# Cell 1 [](#%5F%5Fcodelineno-14-2)import marimo as mo [](#%5F%5Fcodelineno-14-3)import polars as pl [](#%5F%5Fcodelineno-14-4) [](#%5F%5Fcodelineno-14-5)df = pl.DataFrame({ [](#%5F%5Fcodelineno-14-6) "name": ["Alice", "Bob", "Charlie", "David"], [](#%5F%5Fcodelineno-14-7) "age": [25, 30, 35, 40], [](#%5F%5Fcodelineno-14-8) "city": ["New York", "London", "Paris", "Tokyo"] [](#%5F%5Fcodelineno-14-9)}) [](#%5F%5Fcodelineno-14-10) [](#%5F%5Fcodelineno-14-11)age_filter = mo.ui.slider.from_series(df["age"], label="Max age") [](#%5F%5Fcodelineno-14-12)city_filter = mo.ui.dropdown.from_series(df["city"], label="City") [](#%5F%5Fcodelineno-14-13) [](#%5F%5Fcodelineno-14-14)mo.hstack([age_filter, city_filter])
[](#%5F%5Fcodelineno-15-1)# Cell 2 [](#%5F%5Fcodelineno-15-2)filtered_df = df.filter((pl.col("age") <= age_filter.value) & (pl.col("city") == city_filter.value)) [](#%5F%5Fcodelineno-15-3)mo.ui.table(filtered_df)
Select dataframe rows¶
Display dataframes as interactive, selectable charts usingmo.ui.altair_chart ormo.ui.plotly, or as a row-selectable table withmo.ui.table. Select points in the chart, or select a table row, and your selection is automatically sent to Python as a subset of the original dataframe.
Select rows in a table, get them back as a dataframe
pandaspolarslive example
[](#%5F%5Fcodelineno-17-1)# Cell 1 - display a dataframe [](#%5F%5Fcodelineno-17-2)import marimo as mo [](#%5F%5Fcodelineno-17-3)import pandas as pd [](#%5F%5Fcodelineno-17-4) [](#%5F%5Fcodelineno-17-5)df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) [](#%5F%5Fcodelineno-17-6)table = mo.ui.table(df, selection="multi") [](#%5F%5Fcodelineno-17-7)table
[](#%5F%5Fcodelineno-18-1)# Cell 2 - display the selection [](#%5F%5Fcodelineno-18-2)table.value
[](#%5F%5Fcodelineno-19-1)# Cell 1 - display a dataframe [](#%5F%5Fcodelineno-19-2)import marimo as mo [](#%5F%5Fcodelineno-19-3)import polars as pl [](#%5F%5Fcodelineno-19-4) [](#%5F%5Fcodelineno-19-5)df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}) [](#%5F%5Fcodelineno-19-6)table = mo.ui.table(df, selection="multi") [](#%5F%5Fcodelineno-19-7)table
[](#%5F%5Fcodelineno-20-1)# Cell 2 - display the selection [](#%5F%5Fcodelineno-20-2)table.value
Dataframe panels¶
Dataframe outputs in marimo come with several panels to help you visualize, explore, and page through your data interactively. These panels are accessible via toggles at the bottom-left of a dataframe output. If you need further control, after opening a panel you can
- pin the panel to the side of your editor for persistent access;
- toggle focus to automatically display the currently focused dataframe in the panel. Note
Toggles are visible when editing notebooks (with marimo edit ...) but not when running notebooks as apps (with marimo run ...), except for the row viewer which is available in both.
Row viewer panel¶
To inspect individual rows, open the row viewer. This presents a vertical view of the selected row.
- Press
Spaceto select/deselect the current row - Use arrow keys (
←→) to navigate between rows - Click on any row in the dataframe to view its data in the panel
Column explorer panel¶
To explore your data, open the column explorer where you can find summary statistics and charts for each column. Click the + button to add the chart code to a new cell.
This requires the altair package to be installed. For large dataframes, vegafusion is also needed to render charts. To use the generated Python code, enable vegafusion in your notebook:
[](#%5F%5Fcodelineno-22-1)import altair [](#%5F%5Fcodelineno-22-2) [](#%5F%5Fcodelineno-22-3)altair.data_transformers.enable("vegafusion")
Chart builder¶
The chart builder toggle lets you rapidly develop charts using a GUI, while also generating Python code to insert in your notebook. Refer to the chart builder guide for more details.
Preferences¶
When you run a SQL cell in marimo, you can get the output returned as a dataframe. If you have a preference for a specific dataframe library as a default you can configure the "default SQL output" in the user settings by going to the "Runtime" tab.
Configure the default SQL output
Alternatively you can also use the marimo configuration file to configure the default SQL output.
[](#%5F%5Fcodelineno-23-1)[runtime] [](#%5F%5Fcodelineno-23-2)default_sql_output = "native"
Example notebook¶
For a comprehensive example of using Polars with marimo, check out our Polars example notebook.
Run it with:
[](#%5F%5Fcodelineno-24-1)marimo edit https://raw.githubusercontent.com/marimo-team/marimo/main/examples/third_party/polars/polars_example.py