

# Work with cells
<a name="work-with-cells"></a>

## Overview
<a name="work-with-cells-overview"></a>

Notebooks support a variety of languages such as Python, SQL, and Markdown. Each cell is associated with a language, and the editor in the cell supports functionality such as auto code complete, formatting, and linting.

All the code in the notebook is executed on the notebook kernel, which is built on IPython. The notebook kernel runs on SageMaker notebook compute, which has a configurable form factor that includes different types of instances. When cells are executed, they may produce output that is shown below each cell. Notebooks support rich rendering of data frames (pandas or Spark), where the output is rendered in an interactive data table and charts.

## Procedure
<a name="work-with-cells-procedure"></a>

To create and run Python code:

1. Choose **Python** to add a new Python cell.

1. Enter your Python code in the cell editor.

1. Choose the play icon or press **Shift\$1Enter** to run the cell.

1. View the results displayed below the cell.

All notebook code is executed in the notebook kernel, which runs on SageMaker compute. You can configure the form factor of this compute. The notebook runs an IPython kernel that can execute Python code. For larger scale data processing, the notebook's Python environment comes with Spark without requiring you to configure or manage any infrastructure. You can start writing Spark code to run interactive analytics and exploration on serverless, autoscalable Athena Spark.

To create and run SQL code:

1. Choose **SQL** to add a new SQL cell.

1. Select your data connection from the dropdown if prompted.

1. Enter your SQL query in the cell editor.

1. Choose the play icon or press **Shift\$1Enter** to run the cell.

1. View the query results in the interactive table below the cell.

SQL cells can query your existing Python data frames using DuckDB, or run SQL against Athena (SQL), Athena Spark, or any other connection to first-party and third-party engines like Amazon Redshift, Snowflake, and BigQuery. Add a connection from the [available connections that are supported](https://docs.aws.amazon.com/sagemaker-lakehouse-architecture/latest/userguide/lakehouse-data-connection.html).

### Run multiple SQL statements in a single cell
<a name="work-with-cells-multi-statement-sql"></a>

You can write and run multiple SQL statements in a single SQL cell by separating each statement with a semicolon (`;`). When you run a cell that contains multiple statements, the notebook executes them sequentially and displays the results in a tabbed interface.

To run multiple SQL statements:

1. In a SQL cell, enter two or more SQL statements separated by semicolons. For example:

   ```
   SELECT COUNT(*) FROM customers WHERE region = 'US';
   SELECT COUNT(*) FROM customers WHERE region = 'EU';
   SELECT COUNT(*) FROM customers WHERE region = 'ASIA';
   ```

1. Choose the play icon or press **Shift\$1Enter** to run the cell.

1. The output area displays a row of tabs labeled **Result 1**, **Result 2**, **Result 3**, and so on — one tab for each statement.

1. Choose a tab to view the results for that statement.

Each tab displays a status indicator:
+ A success indicator (✓) when the statement completed successfully.
+ An error indicator (⚠) when the statement encountered an error. Choose the tab to view the error details.

### Reference multi-statement SQL results in Python
<a name="work-with-cells-multi-statement-variables"></a>

When you run multiple SQL statements in a single cell, the notebook creates a separate data frame variable for each statement result. The variables follow this naming convention:
+ `dataframe_name` — A list containing all results.
+ `dataframe_name_0` — The result of the first statement.
+ `dataframe_name_1` — The result of the second statement.
+ `dataframe_name_2` — The result of the third statement, and so on.

For example, if the cell's data frame variable is `df_cell_1`, the following variables are created:


| Variable | Description | 
| --- | --- | 
| df\$1cell\$11 | A list containing all statement results | 
| df\$1cell\$11\$10 | Result of the first statement (Result 1 tab) | 
| df\$1cell\$11\$11 | Result of the second statement (Result 2 tab) | 
| df\$1cell\$11\$12 | Result of the third statement (Result 3 tab) | 

You can reference these variables in subsequent Python cells:

```
# Access the first statement's result
print(df_cell_1_0.head())

# Access by index from the list
print(df_cell_1[1].head())
```

You can rename the base data frame variable using the variable name editor in the cell's symbol bar. When you rename the base variable, all indexed variables update automatically. For example, renaming `df_cell_1` to `sales_data` updates the indexed variables to `sales_data_0`, `sales_data_1`, and so on.

**Note**  
Individual indexed variables (such as `sales_data_0`) cannot be renamed independently. Only the base data frame name can be changed.

### Visualize multi-statement results
<a name="work-with-cells-multi-statement-visualize"></a>

Each result tab supports the full set of visualization options independently. You can:
+ Switch between table and chart views for each result tab separately.
+ Apply column filters, sorting, and pinning per result tab.
+ Create different chart types for different statement results.

Visualization settings for the first result tab are saved with the notebook. Settings for additional result tabs are maintained during your current session but reset when you reopen the notebook.

### Rename cells
<a name="work-with-cells-rename"></a>

You can assign custom names to cells to make them easier to identify and navigate, especially in large notebooks. By default, cells are labeled with sequential numbers (1, 2, 3, and so on).

To rename a cell:

1. Choose the cell number in the cell header. The number becomes an editable text field.

1. Enter a custom name for the cell.

1. Press **Enter** or choose outside the text field to save the name.

The custom name replaces the default number in the cell header. This is useful for:
+ Identifying the purpose of specific cells at a glance (for example, "Data cleanup" or "Model training").
+ Navigating large notebooks more efficiently.
+ Making notebooks easier for collaborators to understand.

### Keyboard shortcuts
<a name="work-with-cells-keyboard-shortcuts"></a>

Amazon SageMaker Unified Studio notebooks provide keyboard shortcuts that enable you to perform common actions and navigate within your notebooks. These shortcuts are familiar to developers who have used other Python or SQL notebook editors.

To view all available shortcuts, press **⌘\$1Shift\$1/** (Mac) or **Ctrl\$1Shift\$1/** (Windows/Linux).

Notebooks support two modes for keyboard shortcuts: edit mode and command mode. Shortcuts without modifier keys are generally used in command mode.

#### Edit mode
<a name="work-with-cells-edit-mode"></a>

Edit mode is active when you are editing the content of a cell. In this mode, keyboard shortcuts require modifier keys to avoid interfering with normal text input.


| Action | Mac | Windows/Linux | 
| --- | --- | --- | 
| Run the current cell | ⌘\$1Enter | Ctrl\$1Enter | 
| Run cell and select next cell | Shift\$1Enter | Shift\$1Enter | 
| Toggle Gen AI prompting | Option\$1A | Alt\$1A | 

#### Command mode
<a name="work-with-cells-command-mode"></a>

To enter command mode, press **Esc**. A blue ring appears around the selected cell to indicate that command mode is active.

In command mode, the following actions are available in addition to the edit mode shortcuts:

**Navigation**


| Action | Shortcut | 
| --- | --- | 
| Select above cell | Up | 
| Select below cell | Down | 
| Edit cell content | Enter | 

**Cell creation**


| Action | Shortcut | 
| --- | --- | 
| Add cell above | A | 
| Add cell below | B | 
| Add Python cell | P | 
| Add SQL cell | Q | 
| Add Markdown cell | M | 
| Add table cell | T | 
| Add charts cell | C | 

**Cell operations**


| Action | Mac | Windows/Linux | 
| --- | --- | --- | 
| Copy cell | ⌘\$1C | Ctrl\$1C | 
| Paste cell | ⌘\$1V | Ctrl\$1V | 
| Duplicate cell | ⌘\$1D | Ctrl\$1D | 
| Run cell and insert cell below | Option\$1Enter | Alt\$1Enter | 
| Save notebook | ⌘\$1S | Ctrl\$1S | 

**Tip**  
When the intercell menu is open, you can use the typed cell shortcuts (**P**, **Q**, **M**, **T**, **C**) to add a cell of a specific type.

To add documentation:

1. Choose **Markdown** to add a Markdown cell.

1. Enter your documentation using Markdown syntax.

1. Choose the play icon or press **Shift\$1Enter** to render the formatted text.

To reference data between cells: Python variables created in one cell are available in subsequent cells. SQL query results can be referenced by variable name in Python cells. You can also use the variable explorer in the left navigation to see all available variables and their schemas.