Keyword cell_output

The cell_output keyword is used in cells of type Python to refer to the result of the previous cell of type SQL. With this keyword it is convenient to switch from SQL to Python when working with data inside one notebook.

Data in cell_output is written as Pandas DataFrame.

The cell_output keyword is interpreted on a per-cell basis, not for the entire notebook. This means that the values written to cell_output may be different in different cells of type Python within the same notebook depending on the previous cells of type SQL. Therefore, it is recommended that the contents of the cell_output variable be written to a new variable if you intend to work with this data in multiple Python cells (as shown in the example below).

Example

Let’s run a cell of type SQL with a SELECT query to output numbers from 1 to 5. For this purpose we will use the functions unnest and generate_series:

SELECT unnest(generate_series(1,5)) as numbers;
+---------+
| numbers |
+---------+
| 1       |
+---------+
| 2       |
+---------+
| 3       |
+---------+
| 4       |
+---------+
| 5       |
+---------+

In the next cell of type Python of the same notebook, we refer to the table output in the previous cell via the cell_output keyword:

# Print the value of the cell_output variable
print(cell_output)
print()

# Print the type of the cell_output variable
print(type(cell_output))
print()

# Write the value of the cell_output variable to a new variable
df = cell_output

# Filter values in DataFrame by column condition using the new variable
print(df.loc[df['numbers'] > 2])
numbers
0        1
1        2
2        3
3        4
4        5

<class 'pandas.core.frame.DataFrame'>

   numbers
2        3
3        4
4        5

We can see that the content of the cell_output variable in this cell is a DataFrame with data taken from the result of the previous cell. You can work with this DataFrame using any standard Python tools.


Real examples of using the cell_output keyword can be seen in the scripts:

* Instagram belongs to Meta, a company recognised as extremist and banned in the Russian Federation.