【问题标题】:Spotfire add column from python listSpotfire 从 python 列表中添加列
【发布时间】:2015-12-03 14:39:34
【问题描述】:

如何将 python 列表作为新列添加到 spotfire 数据表中。例如,我希望添加一个具有使用 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 方法的唯一示例涉及添加从另一个文件读取的列,我不知道如何使它们工作。

【问题讨论】:

    标签: ironpython spotfire


    【解决方案1】:

    我已经破解了它,并从这个相关解决方案中获得了一些指导:How to create a data table on the fly in Spotfire via python

    简短的回答是,您不能直接从 python 列表中添加一列,但您可以创建一个文本对象并直接从中导入,而无需将其保存在任何地方。

    这是脚本的外观。

    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)
    

    这将创建一个名为“计算值”的新列。

    【讨论】:

    • 我在最后一行收到以下错误:“TypeError: AddColumns() 恰好需要 3 个参数(给定 2 个)”这是没有意义的,因为在 Spotfire 文档中 AddColumns() 函数确实需要您提供的两个参数 :( 除非从 7.7.x 更改为 7.11.0
    • @alex 是其余代码作为我的示例吗? “恰好采用 N 个参数(给定 N-1)”通常与对象的自身对象不存在有关,所以我想知道您是否尝试调用 AddColumns() 而不是 DataTable.AddColumns()?
    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2018-07-29
    • 2020-08-28
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多