在Spotfire中,我有两个带有共享列“ id”的表“ A”和“ B”。令x为A中标记行的“ id”值的集合。我想向B添加新列“ flag”,使得:
有人可以帮我编写一个脚本(我猜是IronPython)来实现吗?这是一个示例(a,b,c和x,y,z列无关紧要):
A: id a b c
1*
2
2*
3
3
(*为标记行)
B: id x y z
1
1
2
2
2
3
3
应导致:
B: id x y z flag
1 1
1 1
2 1
2 1
2 1
3 0
3 0
答案 0 :(得分:2)
由于niko的评论和以下示例脚本,我找到了解决方案:
这是主意:
我手动创建了带有“ 0”和“ 1”标签的“标志”列。我不确定这是否也可以自动化。
这是完整的脚本:
sourceTableName = 'A'
sourceMarkingName = 'Marking'
sourceTableIDColumnName = 'id'
targetTableName = 'B'
targetMarkingName = 'Marking (2)'
targetTableIDColumnName = 'id'
targetTableFlagColumnName = "flag"
from System.Collections.Generic import List
from Spotfire.Dxp.Data import *
dataManager = Application.Document.Data
sourceTable = Document.Data.Tables[sourceTableName]
sourceMarking = dataManager.Markings[sourceMarkingName]
targetTable = Document.Data.Tables[targetTableName]
targetMarking = dataManager.Markings[targetMarkingName]
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
# step 1: get set of id values marked in source table
#
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Create a cursor for the table column to get the values from
sourceCursor = DataValueCursor.CreateFormatted(sourceTable.Columns[sourceTableIDColumnName])
# Create a List object to store the values for the rows marked in the source table
markedData = List[str]();
# Iterate through the source data table rows to retrieve the marked values
for row in sourceTable.GetRows(sourceMarking.GetSelection(sourceTable).AsIndexSet(), sourceCursor):
value = sourceCursor.CurrentValue
if value <> str.Empty:
markedData.Add(value)
# Get the unique values
idValues = sorted(List[str](set(markedData)))
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
# step 2: use the id values to mark rows in target table
#
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Set marking of all rows in target table to False
rowsToSelect = IndexSet(targetTable.RowCount, False)
targetCursor = DataValueCursor.CreateFormatted(targetTable.Columns[targetTableIDColumnName])
# Iterate through the target table rows to set the marked rows
rowIndex = 0
for row in targetTable.GetRows(targetCursor):
value = targetCursor.CurrentValue
rowsToSelect[rowIndex] = value in idValues
rowIndex += 1
# Set marking on targetTable
targetMarking.SetSelection(RowSelection(rowsToSelect), targetTable)
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
# step 3: assign tag '1' to the marked rows and '0' to all other rows in the target table
#
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Get handle to the required column and typecast it to a tagcolumn
myTagColumn = targetTable.Columns.Item[targetTableFlagColumnName].As[TagsColumn]()
# get index to all rows
allRows = IndexSet(targetTable.RowCount, True)
# assign tags
myTagColumn.Tag('0', RowSelection(allRows)) # assign '0' to all rows
myTagColumn.Tag('1', targetMarking.GetSelection(targetTable)) # assign '1' to marked rows