Skip to main content

Cell-type User Operators

User operators with the Cell type are the simplest kind of user operator. Much of the functionality available to the built-in Python cell operator is available to Cell-type user operators, although any code in the operator does not run in the canvas's global Python runtime. Below are examples of the code for two Cell-type user operators.

Example 1: Transpose

This operator returns the transpose of an input dataframe.

df[attributes["columns"]].transpose().reset_index()

Example 2: One-hot encoding

This operator takes a dataframe and one of its columns as input, and applies one-hot encoding to the input column.

import pandas as pd

dummies = pd.get_dummies(df[[attributes['column'][0]]])
pd.concat([df, dummies], axis=1) if params['append'] else dummies

Creating Cell-type User Operators

When creating user operators with the Cell type, there are a few things to keep in mind:

  • The input dataframe (if there is only one) can be accessed through the df keyword
  • If there are multiple input dataframes, they can be accessed through the dfs list keyword
  • The operator inputs can be accessed in code using the attributes (for column/attribute selection) and params (for all other input types) keywords.
  • The last line of the code (in the Script tab) defines what is returned from the operator

Also note that there is no progressiveness, so the entire dataset will be processed at once.

Python Visualizations

All Python-based visualization functionality (e.g. through matplotlib) supported by the Python cell operator is also supported in Cell-type user operators. Visit the Python cell page for information.