Analyze data with Python Lab
Python Lab runs Python next to your data, in a sandbox with pandas and the usual analysis libraries ready. Use it when a question needs more than SQL: reshaping, statistics, forecasting, or a model.
The screen
- Dataframes & Sources
- The left panel: add SQL DataFrames and see the ones already staged.
- The toolbar
- Run, Save, Dataset, Scripts, script tabs, Format and Export.
- The editor
- Your script. Line numbers, Python highlighting and suggestions.
- Under the editor
- Run Script and this workspace's run limits.
- Results
- Results and Execution Log for the last run.
Stage data as DataFrames
Scripts read data you stage first: the result of a SQL query, loaded as a pandas DataFrame.
Choose the source
In Dataframes & Sources, under Add SQL DataFrame, pick a Database and Schema.
Name it and write the query
Enter a Variable Name such as
orders, and the SQL Query that returns the rows you need. Preview SQL Results shows the first rows before you commit.Stage it
Choose Stage DataFrame. It appears under Staged DataFrames with its connection, ready for every run. Use the buttons beside it to edit its query or name, run it again, or remove it.
In your script, a staged DataFrame is available under its own name and in the tables dictionary:
orders.head() # by its variable name
tables["orders"].shape # or through tablesWrite and run a script
Write Python in the editor and choose Run Script, or press Ctrl+Enter. Output from print and any errors appear under Execution Log.
import pandas as pd
df = tables["orders"]
margins = df.groupby("category")[["revenue", "profit"]].sum()
margins["margin"] = (margins.profit / margins.revenue).round(3)
margins = margins.sort_values("margin", ascending=False).reset_index()
print(f"{len(df):,} order lines across {df.category.nunique()} categories")
lab.output("Margin by category", margins)- Format
- Tidy the script's layout.
- Export
- Download the script as a .py file.
- Script tabs
- Work on several scripts at once.
Show results with lab.output
lab.output(name, value) adds a result to the Results tab. What it shows depends on the value:
| You pass | You get |
|---|---|
| A pandas DataFrame | A table you can search, download as CSV and save as a dataset |
| A Matplotlib or seaborn figure | An image (PNG) |
| A Plotly figure | An interactive chart |
| Text | A text result |
| Anything else (lists, dictionaries, numbers) | A JSON result |
If a script calls lab.output nowhere and leaves a DataFrame called df, that DataFrame is shown automatically. In a table result, click any cell to copy its value; the download button saves the table as CSV.
Save and reopen scripts
Choose Save, give the script a Script Name and an optional Description, and choose Save Script. Saving again keeps the earlier version too. Scripts lists your saved scripts; choose one to open it.
Save a result as a dataset
A table result can become a dataset, so the numbers you worked out in Python can be charted, put on dashboards and asked about.
Run the script
Make sure the result you want is in the Results tab.
Choose Dataset
The dialog shows the result's shape, such as 5 rows • 4 columns, and its column names. Enter a Dataset Name and a description.
Choose how it's stored and save
Python results are always stored as an import (DuckDB and Parquet), so charts read them quickly without running Python. Choose Save Dataset.
Packages
A runtime environment pins the packages a script can import. They include pandas, NumPy, SciPy, scikit-learn, statsmodels, Polars, Matplotlib, seaborn and Plotly. Your admins can create environments with other versions and choose the default.
Sandbox limits
Every run is isolated. Scripts can't reach the network, start other programs or read files outside the run, so they only see the data you staged. Under the editor, Python Lab shows this workspace's limits, for example Runs stop after 10 s · 512 MB of memory · code up to 20,000 bytes · 5 runs at once. Admins change them under Governance and guardrails.
When row or column rules cover you
Python Lab reads connections directly, so if a security rule covers you on a connection, staging SQL from it is refused. Work from the datasets, charts and dashboards built on it instead, where the rules are applied. Roles that need raw access can be given the permission to bypass row and column security.