Spotfire add column from python list

限于喜欢 提交于 2020-01-24 12:04:25

问题


How would one add a python list to a spotfire data table as a new column. For example I wish to add a column that has values calculated using python.

from Spotfire.Dxp.Data import *

# Get the data table
DataTable = Document.Data.Tables.TryGetValue("Table Name")[1]

# define some cursors
CursorA = DataValueCursor.CreateFormatted(DataTable.Columns["Column A"])
CursorB = DataValueCursor.CreateNumeric(DataTable.Columns["Column B"])
CursorC = DataValueCursor.CreateNumeric(DataTable.Columns["Column C"])

# define a list
NewColumnValues = []

# Go row by row and calculate the values I want.
for row in DataTable.GetRows(CursorA, CursorB, CursorC):
    A = CursorA.CurrentValue
    B = CursorB.CurrentValue
    C = CursorC.CurrentValue
    V = SomeComplicatedFunction(A, B, C)
    NewColumnValues.append(V)

# And now add that column to the datatable
# If only it would work like this...
DataTable.AddColumns('NewColumnName', NewColumnValues)

Is there a way to do that? The only examples I can see that use the AddColumns method involve adding a column read from another file, and I can't see how to make them work.


回答1:


I've cracked it, with some guidance from this related solution: How to create a data table on the fly in Spotfire via python

The short answer is that you cannot add a column from a python list directly, but you can create a text object and import directly from that without having to save it anywhere.

Here's how the script looks.

from Spotfire.Dxp.Data import *
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data.Import import *

# Get the data table
DataTable = Document.Data.Tables.TryGetValue("Table Name")[1]

# define some cursors
CursorA = DataValueCursor.CreateFormatted(DataTable.Columns["Column A"])
CursorB = DataValueCursor.CreateNumeric(DataTable.Columns["Column B"])
CursorC = DataValueCursor.CreateNumeric(DataTable.Columns["Column C"])
CursorRowId = DataValueCursor.CreateNumeric(DataTable.Columns["Row ID"])
# Note that column "Row ID" must be a unique identifier, and it can't be a calculated field (even a frozen one).

textData = ""
# Go row by row and calculate the values I want.
    for row in DataTable.GetRows(CursorA, CursorB, CursorC, CursorRowId):
    A = CursorA.CurrentValue
    B = CursorB.CurrentValue
    C = CursorC.CurrentValue
    V = SomeComplicatedFunction(A, B, C)
    textData += "%d\t%f\r\n" % (CursorRowId, V)

# So now textData is a two column text string containing all the data I need.
# Turn it into an in-memory text data source.

stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

readerSettings = TextDataReaderSettings()
readerSettings.Separator = "\t"
readerSettings.SetDataType(0, DataType.String)
readerSettings.SetColumnName(0, 'Row ID')
readerSettings.SetDataType(1, DataType.Real)
readerSettings.SetColumnName(1, 'Calculated Value')

NewInfo = TextFileDataSource(stream, readerSettings)

# Define the table relationship
leftColumnSignature = DataColumnSignature("Row ID", DataType.Integer)
rightColumnSignature = DataColumnSignature("Row ID", DataType.Integer)
columnMap = {leftColumnSignature:rightColumnSignature}
ignoredColumns = []
columnSettings = AddColumnsSettings(columnMap, JoinType.LeftOuterJoin, ignoredColumns)

# Add the column
DataTable.AddColumns(NewInfo, columnSettings)

And that will create a new column called "Calculated Value".



来源:https://stackoverflow.com/questions/34068783/spotfire-add-column-from-python-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!