C3 AI Documentation Home

Python

Use Visual Notebooks to transform data using custom Python. For more information about relevant Python functions, view the documentation for the following Python libraries:

Configuration

FieldDescription
Name default=noneField to name the node A user-specified node name, displayed in the canvas and in the dataframe as a tab.
Group by default=No groups - data is single seriesGrouping selection Select whether to to view the data in a single series or grouped by data in another column.
Select column to partition with default=noneColumn to group by If Group data by column is selected for the Group by field, select a column to partition the data.
Output Name default=outputName of the output dataframe Enter a name for the dataframe that contains the output of the custom Python function. This must match the name of the output variable in the function.

Node Inputs/Outputs

InputA Visual Notebooks dataframe
OutputA dataframe transformed by one or more Python functions

Example notebook Figure 1: Example notebook

Example dataframe output Figure 2: Example dataframe output

Examples

The data shown in Figure 3 is used in this example. It contains HR analytics for employees of a particular company. We would like to group Age into 10-year buckets as a data preprocessing step.

Example input data

Figure 3: Example input data

Follow the steps below to create the age buckets using a custom Python function:

  1. Connect a Python node to the example input data. Ensure output is entered in Output Name.

  2. Copy the code below and paste it into the Notebook tab, as shown in Figure 4. Click Run at the top right of the notebook.

    • If you enter an invalid Python function, Visual Notebooks presents an error.
    • Notice that Visual Notebooks displays a list of column names and types as a reference. output = input labels = ["{0} - {1}".format(i, i + 9) for i in range(0, 100, 10)] output['Age_Group'] = pd.cut(output['Age'], range(0, 105, 10), right=False, labels=labels).astype(pd.StringDtype())
  3. Click Run at the bottom of the configuration sidebar to create the Age_Group column with the output of the custom Python function.

Example notebook with custom Python function Figure 4: Example notebook with custom Python function

Example dataframe with the output of the custom Python functio

Figure 5: Example dataframe with the output of the custom Python function

Was this page helpful?