使用列名切换我行中的第一组值 (IronPython)

Switching the first set of values in my row with the column names (IronPython)

所以我的数据 table 结构类似于

ColumnName1 | ColumnName2 | ColumnName3
------------|-------------|------------
value1      |value2       |value2

我如何将我的列名更改为值,所以如果我想将 "ColumnName1" 更改为 "value1" 我将如何使用 ironpyton 脚本。到目前为止我所知道的是如何遍历列名:

for col in Document.Data.Tables[myTable].Columns:

但我不确定如何切换数据。然后删除第 1 行。

将 dataTable 作为数据 table 参数。首先,让我们获取新的列名:

# Imports
from Spotfire.Dxp.Data import *

cursor1 = DataValueCursor.CreateFormatted(dataTable.Columns[0])
cursor2 = DataValueCursor.CreateFormatted(dataTable.Columns[1])
cursor3 = DataValueCursor.CreateFormatted(dataTable.Columns[2])

#iterate through table column rows to retrieve the value of the first line
for row in dataTable.GetRows(cursor1,cursor2,cursor3):
    value1 = cursor1.CurrentValue
    value2 = cursor2.CurrentValue
    value3 = cursor3.CurrentValue
    break

然后检索当前列名并用新列名替换它们:

oldColName1 = dataTable.Columns[0].Name
oldColName2 = dataTable.Columns[1].Name
oldColName3 = dataTable.Columns[2].Name
dataTable.Columns[0].Name = value1
dataTable.Columns[1].Name = value2
dataTable.Columns[2].Name = value3

现在让我们用旧的列名替换值(如果不需要此功能,可以删除这部分)

for row in dataTable.GetRows(cursor1,cursor2,cursor3):
    cursor1.CurrentValue = oldColName1
    cursor2.CurrentValue = oldColName2
    cursor3.CurrentValue = oldColName3
    break

在评论中编辑操作请求

# Imports
from Spotfire.Dxp.Data import *

cursors = []
nbcol = dataTable.Columns.Count

for x in range(nbcol):
    cursors.append(DataValueCursor.CreateFormatted(dataTable.Columns[x]))


#iterate through table column rows to retrieve the value of the first line
values = []

for row in dataTable.GetRows(*cursors):
    for x in range(nbcol):
        values.append(cursors[x].CurrentValue)
    break

oldColNames = []

for x in range(nbcol):
    oldColNames.append(dataTable.Columns[x].Name)
    dataTable.Columns[x].Name = values[x]

for row in dataTable.GetRows(*cursors):
    for x in range(nbcol):
        cursors[x].CurrentValue = oldColNames[x]
    break