Jump to content
  • Appending Rows to a Table and Creating a New Column to Keep Track of the Origin of the Data in Spotfire® Using IronPython Scripting


    In the Analyst client, you have the option to add rows from another table/file/source and have a column to identify the origin of new rows. Below is an example of merging three tables into one. A new table is created and the first table is copied over to that table. The second table is then merged into the new table and a new column is created keeping track of the origin of the rows. The third table is then merged into the new table and using the already existing column to update the origin of the added rows.

    Introduction

    In the Analyst client, you have the option to add rows from another table/file/source and have a  column to identify the origin of new rows. Below is an example of merging three tables into one. A new table is created and the first table is copied over to that table. The second table is then merged into the new table and a new column is created keeping track of the origin of the rows.  The third table is then merged into the new table and using the already existing column to update the origin of the added rows.

    Code Sample

    # Copyright © 2017. TIBCO Software Inc. Licensed under TIBCO BSD-style license.
    from Spotfire.Dxp.Data import *
    from Spotfire.Dxp.Data.Import import DataTableDataSource
    
    #Define new table name
    newTableName = 'AllTables'
    
    #Define the source table
    inputTableDS = DataTableDataSource(Document.Data.Tables["A"])
    
    #Function to return a Spotfire Data Table. Will return None if the data table does not exist
    #parameter: tableName - the name of the data table in the Spotfire document
    def getDataTable(tableName):
        try:
            return Document.Data.Tables[tableName]
        except:
            print ("Cannot find data table: " + tableName + ". Returning None")
            return None
    
    #Check if new table already exists
    dt = getDataTable(newTableName)
    if dt != None:
        #If exists, replace it
        dt.ReplaceData(inputTableDS)
    else:
        #If it does not exist, create new
        Document.Data.Tables.Add(newTableName, inputTableDS)
    
    # The table where all other sources will be appended.
    outputTable = Document.Data.Tables[newTableName]
    
    # Input Table Data Source, Add row settings to create new column and set values depending on sources, then append the new rows
    inputTableDS = DataTableDataSource(Document.Data.Tables["B"])
    addRowsSettings = AddRowsSettings(outputTable, inputTableDS, "Origin", "Table B", "Table A")
    outputTable.AddRows(inputTableDS, addRowsSettings)
    
    # Input Table Data Source, Add row settings to use existing column and set values depending on source, then append the new rows
    inputTableDS = DataTableDataSource(Document.Data.Tables["C"])
    addRowsSettings = AddRowsSettings(outputTable, inputTableDS, "Origin", "Table C")
    outputTable.AddRows(inputTableDS, addRowsSettings)
     

    References

    License:  TIBCO BSD-Style License

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...