Accessing Output Datasets
To access the data in Python's output ports, use dedicated objects: OutputTable and OutputTables[]:
- Use the
Nindex of a corresponding Output port N to distinguish data sources:OutputTables[N] - The first output data port, present by default, has index
0, not displayed in the port's name. (I.e., the port name shown is just Output dataset.) To access data in this default port, use a special object:OutputTable
OutputTable properties
Columns
Contains the read-only iterable collection of columns, implements the Mapping and Sequence protocols, and returns a value of type ColumnsClass. Elements can be accessed via bracket notation [] by names and by indexes. If the Allow creating output columns in script checkbox is marked, the collection elements are of type ConfigurableOutputColumnClass; otherwise, they are OutputColumnClass. Both of these types are inherited from ColumnClass and implement the Sequence protocol (see Full API Description).
ColumnCount
Contains the read-only count of columns in the output dataset. Returns a value of type int.
RowCount
Contains the read-only count of rows in the output dataset. Returns a value of type int.
OutputTable methods
Get(row, col)
row: The row index. Accepts a value of typeint.col: The column index or name. Accepts values of typeintorstr.
The method returns the value of the specified column in the specified row. The returned value can be of the following types: bool, int, float, str, datetime.datetime, or None.
IsNull(row, col)
row: The row index. Accepts a value of typeint.col: The column index or name. Accepts values of typeintorstr.
The method returns a boolean value true if the column in the specified row has a missing value. Otherwise, it returns false.
GetColumn(col)
col: The column index or name. Accepts values of typeintorstr. If the Allow creating output columns in script checkbox is marked, a value of typeConfigurableOutputColumnClassis returned; otherwise,OutputColumnClassis returned. Both of these types are inherited fromColumnClassand implement theSequenceprotocol (see Full API Description).
Append()
This method adds a new row to the output dataset. It has no arguments.
Set(col, value)
col: The column index or name. Accepts values of typeintorstr.value: The value of the following types:bool,int,float,str,datetime.datetime, orNone.
This method sets the value of the specified column in the row added by the Append() method.
AssignColumns(array)
array: An iterable object containing elements of typeColumnInfo(see Full API Description). This method creates columns for the output dataset from a collection ofColumnInfoelements.
AddColumn(ColumnInfo, Name, DisplayName, DataType, DataKind, DefaultUsageType)
Accepts arguments by keyword:
ColumnInfo: A value of typeColumnInfo(see Full API Description). Optional argument.Name: The column name, a value of typestr. Optional argument.DisplayName: The column label, a value of typestr. Optional argument.DataType: The column data type, a value of typeint(see Enumerations). Optional argument.DataKind: The column's data kind, a value of typeint(see Enumerations). Optional argument.DefaultUsageType: Default usage of the column, a value of typeint(see Enumerations). Optional argument.
This method inserts a column at the end of the output set's column list. It returns a value of type OutputColumnClass (see Full API Description).
InsertColumn(Index, ColumnInfo, Name, DisplayName, DataType, DataKind, DefaultUsageType)
Accepts arguments by keywords:
Index: The index of the column in the column collection. Accepts a value of typeint.ColumnInfo: A value of typeColumnInfo(see Full API Description). Optional argument.Name: The column name, a value of typestr. Optional argument.DisplayName: The column label, a value of typestr. Optional argument.DataType: The column's data type, a value of typeint(see Enumerations). Optional argument.DataKind: The column's data kind, a value of typeint(see Enumerations). Optional argument.DefaultUsageType: Default usage of the column, a value of typeint(see Enumerations). Optional argument.
This method inserts a column at the specified index in the output dataset. It returns a value of type OutputColumnClass (see Full API Description).
DeleteColumn(col)
col: The column's index or name. Accepts values of typeintorstr.
This method deletes a column by its name or index.
ClearColumns()
This method clears the column list. It has no arguments.
Using the builtin_pandas_utils module
If the Allow creating output columns in script option is enabled, the following methods are available:
prepare_compatible_table(table, dataframe, with_index)
This method sets the field structure of an OutputTable from a pandas.DataFrame. Arguments:
table: A reference to the outputOutputTableClassdataset.dataframe: A reference to thepandas.DataFramewhose structure is used to create the output dataset columns.with_index: If this argument isTrue, thepandas.DataFrameindexes are included in the structure of the output dataset. This is an optional argument. The default value isFalse.
fill_table(table, dataframe, with_index)
This method writes data from a pandas.DataFrame to an OutputTable. Arguments:
table: A reference to the output dataset. Accepts a value of typeOutputTableClass.dataframe: A reference to thepandas.DataFrame.-
with_index: If this argument isTrue, thepandas.DataFrameindexes are exported to the output dataset. This is an optional argument. The default value isFalse.
You can find an example of using builtin_pandas_utils module below (see Example 2).
Examples
Example 1
from builtin_data import InputTable, OutputTable, DataType, DataKind, UsageType
import datetime
#Copying columns from the input dataset
OutputTable.AssignColumns(InputTable.Columns)
#Deleting a column by index
OutputTable.DeleteColumn(0)
#Deleting a column by name
OutputTable.DeleteColumn("Test1")
#Deleting the entire list of columns
OutputTable.ClearColumns()
#Adding a column to the end of the output dataset's column list
OutputTable.AddColumn(Name="COL0",
DisplayName="Date/Time",
DataType=DataType.DateTime,
DataKind=DataKind.Continuous,
DefaultUsageType=UsageType.Active)
#Inserting a column at a specified index in the output dataset's column list
OutputTable.InsertColumn(Index=0,
Name="COL1",
DisplayName="Flag",
DataType=DataType.Boolean)
#Getting a reference to a column by name
COL0 = OutputTable.GetColumn("COL0")
COL1 = OutputTable.GetColumn("COL1")
#Outputting the column property values
print("Index: ", COL1.Index)
print("Name: ", COL1.Name)
print("DisplayName: ", COL1.DisplayName)
print("DataType: ", COL1.DataType)
print("DataKind: ", COL1.DataKind)
print("DefaultUsageType: ", COL1.DefaultUsageType)
#Adding a row to the output dataset
OutputTable.Append()
#The current Date/Time is written to the field with index 0
OutputTable.Columns[1].Set(datetime.datetime.now())
#The value "true" is written to the field with index 1
OutputTable.GetColumn(0).Set(True)
#Copying the values of the first row to the second
OutputTable.Append()
for i in range(OutputTable.ColumnCount):
value = OutputTable.Get(0, i)
OutputTable.Set(i, value)
#Checking that the value in the row with index 0 in the column with index 1 is not defined
print(OutputTable.IsNull(0, 1))
print(OutputTable.Get(0, 1) is None)
print("RowCount = ", OutputTable.RowCount)
#Output: RowCount = 2
Example 2
Using the builtin_pandas_utils module.
from builtin_data import InputTable, OutputTable, ConfigurableOutputTableClass
from builtin_pandas_utils import to_data_frame, prepare_compatible_table, fill_table
#The input port is optional and may not contain data
if InputTable:
#Create a pd.DataFrame from the input dataset
input_frame = to_data_frame(InputTable)
#Grouping input_frame
output_frame = input_frame.groupby(["Class"]).sum()
#If the "Allow creating output columns in script" option is enabled,
#the structure of the output dataset can be prepared from a pd.DataFrame
assert isinstance(OutputTable, ConfigurableOutputTableClass)
#Defining the structure of the output dataset
prepare_compatible_table(OutputTable, output_frame, with_index=True)
#Filling the output dataset
fill_table(OutputTable, output_frame, with_index=True)
Read on: Python: Enumerations