首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spotfire:如何增加变量来构建评分机制?

Spotfire:如何增加变量来构建评分机制?
EN

Stack Overflow用户
提问于 2019-06-26 17:20:29
回答 1查看 335关注 0票数 1

我正在尝试弄清楚如何使用Spotfire (在线版)中的变量来构建评分机制,并使用最终结果填充计算列。

我有几个存储在列中的值,我将使用它们来计算分数并为其赋值,如下所示:

如果为column1<10,则segment1 = segment1 +1

如果为column1>10,则segment2 = segment2+1

...ETC..。

最后,每个“段”应该有一个分数,我想简单地显示具有最高分数的段的名称。

例如:

Segment1的最终值为10

Segment2的最终值为22

Segment3的最终值为122

我会将Segment3显示为计算列的值

只使用" IF“会使我得到一个复杂的IF结构,所以我更倾向于寻找看起来更像脚本的东西。

有没有办法通过Spotfire实现这一点?

谢谢Laurent

EN

回答 1

Stack Overflow用户

发布于 2019-06-27 00:39:06

要遍历数据行并计算运行分数,可以使用IronPython脚本。下面的脚本从名为"Data Table“的数据表的Col1和Col2中读取数字数据。它计算每一行的分数值,并将其写入制表符分隔的文本字符串。完成后,它使用Add Columns函数将其添加到Spotfire表中。请注意,现有数据需要具有唯一标识符。如果没有,可以使用RowId()函数为唯一的行id创建一个计算列。

代码语言:javascript
运行
复制
from Spotfire.Dxp.Data import *
from System.IO import StringReader, StreamReader, StreamWriter, MemoryStream, SeekOrigin
from Spotfire.Dxp.Data.Import import *
from System import Array

def add_column(table, text, col_name):
    # read the text data into memory
    mem_stream = MemoryStream()
    writer = StreamWriter(mem_stream)
    writer.Write(text)
    writer.Flush()
    mem_stream.Seek(0, SeekOrigin.Begin)

    # define the structure of the text data
    settings = TextDataReaderSettings()
    settings.Separator = "\t"
    settings.SetDataType(0, DataType.Integer)
    settings.SetColumnName(0, 'ID')
    settings.SetDataType(1, DataType.Real)
    settings.SetColumnName(1, col_name)

    # create a data source from the in memory text data
    data = TextFileDataSource(mem_stream, settings)

    # define the relationship between the existing table (left) and the new data (right)
    leftColumnSignature = DataColumnSignature("Store ID", DataType.Integer)
    rightColumnSignature = DataColumnSignature("ID", DataType.Integer)
    columnMap = {leftColumnSignature:rightColumnSignature}
    ignoredColumns = []
    columnSettings = AddColumnsSettings(columnMap, JoinType.LeftOuterJoin, ignoredColumns)

    # now add the column(s)
    table.AddColumns(data, columnSettings)

#get the data table
table=Document.Data.Tables["Data Table"]

#place data cursor on a specific column
cursorCol1 = DataValueCursor.CreateFormatted(table.Columns["Col1"])
cursorCol2 = DataValueCursor.CreateFormatted(table.Columns["Col2"])
cursorColId = DataValueCursor.CreateFormatted(table.Columns["ID"])
cursorsList = Array[DataValueCursor]([cursorCol1, cursorCol2, cursorColId])

text = ""
rowsToInclude = IndexSet(table.RowCount,True)
#iterate through table column rows to retrieve the values
for row in table.GetRows(rowsToInclude, cursorsList):
    score = 0
    # get the current values from the cursors
    col1Val = cursorCol1.CurrentDataValue.ValidValue
    col2Val = cursorCol2.CurrentDataValue.ValidValue
    id = cursorColId.CurrentDataValue.ValidValue
    # now apply rules for scoring
    if col1Val <= 3:
        score -= 3
    elif col1Val > 3 and col2Val > 50:
        score += 10
    else:
        score += 5
    text += "%d\t%f\r\n" % (id, score)

add_column(table, text, 'Score_Result')

对于没有脚本但也没有累积的方法,可以使用计算列。要获得分数,可以将计算列与case语句一起使用。对于段1,您可能有:

代码语言:javascript
运行
复制
case 
when [Col1] > 100 then 10
when [Col1] < 100 and [Col2] > 600 then 20
end

一旦你有了分数,你就可以创建一个计算列,比如MaxSegment。其表达式为Max(Segment1,Segment2,Segment3...)。然后显示MaxSegment的值。

本例中的max函数充当行表达式,并计算给定列的各行的最大值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56769335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档