Spotfire从python列表中添加列 [英] Spotfire add column from python list

查看:126
本文介绍了Spotfire从python列表中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将python列表作为新列添加到Spotfire数据表中.例如,我希望添加一列,其值使用python计算得出.

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)

有没有办法做到这一点?我可以看到的唯一使用AddColumns方法的示例涉及添加从另一个文件读取的列,而我看不到如何使它们工作.

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.

推荐答案

在相关解决方案的一些指导下,我已将其破解:

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

简短的答案是,您不能直接从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.

这是脚本的外观.

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".

这篇关于Spotfire从python列表中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆