Can I modify an entire column from a dataset with system.dataset.setValue()?

Hi, I need to modify an entire colum from a dataset. I'm trying to do it with a cycle 'for' and 'system.dataset.setValue' but the cycle for only modified the last cell of the colum

This is my example code:

table = event.source.parent.getComponent("Table")
dataIn = system.dataset.toPyDataSet(table.data)

values=[10,20,30]

for n in range(0,2):
	table.data=system.dataset.setValue(dataIn, n, "columnIntegers", values[n])
	print( n ,"times")

When I run the script, only edited the last cell, which has the int 30
And I checked the Output console and show

0 times
1 times
2 times
I think system.dataset.setValue is not the best way but I'm noob on this
Y tried doing the cycle 'manual' and I obtained the same result
how can I code it?

system.dataset.setValue() doesn't modify the source dataset. It makes a new dataset with the change request. You would need to re-assign to dataIn within the loop, and then assign dataIn to the target property after the loop is done.

That aside, this is terribly inefficient. I recommend using system.dataset.filterColumns() to drop the old column completely, then system.dataset.addColumn() to add it back with the new data. Construct just the new data in the loop (or perhaps in a list comprehension).

5 Likes
def mapColumn(function, dataset, column):
	if isinstance(column, basestring):
		column = dataset.getColumnIndex(column)

	columnData = map(function, dataset.getColumnAsList(column))
	columnsToKeep = range(dataset.columnCount)
	columnsToKeep.pop(column)
	dsWithoutColumn = system.dataset.filterColumns(dataset, columnsToKeep)
	
	return system.dataset.addColumn(
		dsWithoutColumn, 
		column, 
		columnData, 
		dataset.getColumnName(column), 
		dataset.getColumnType(column)
	)

It's relatively easy to create a 'mapColumn' function composed of our base primitives. This will be pretty efficient.
Usage is pretty simple - just declare a function that performs the calculation you want, for each value in the column, then supply it (no parenthesis!) to the mapColumn function:

# usage:
def updateColumn(value):
	return value * 2

updatedDs = mapColumn(updateColumn, ds, "b")

On my test dataset, this does what you'd expect:

a | b | c
1 | 2 | 3
4 | 5 | 6

a | b | c
1 | 4 | 3
4 | 10 | 6
5 Likes

Thanks! It works!

Now I'm trying to do something more than Just do a calcule for a specific column.
I have a List with float tags provided from Tags OPC (No problem obtaining this values) and I need
to put them in specific column on the same orden
Column/position0= List[0]
Column/position1=List[1]
...
Column/position n=List[n]

Where List contain the values of tags and 'n' is a knowed parameter

I was trying with a function similar
But I only could put the same value of the List in all rows of the column
I'm sorry if the solution is so obvious and dumb for you. I'm new with this Framework and Python.
Each Time I try to do somethings more complex for me

I try put the list on the first parameter but the error said "list is not calleable"
Then I tried to do a for

for n in range(0,index_knowed):
	updatedDs = mapColumn(values[n], table.data, "GRUESO")
table.data=updatedDs

TypeError: 'float' object is not callable

Values is a list with float values.

I tried to make
integer_Values[n]= int(values[n])
inside the for but
The error was
TypeError: 'int' object is not callable

Can anyone guide me?

Is this already in the dataset, or are you adding a column to the dataset?

I saw my error, the differences between the example code posted for @PGriffith and what I needed it, it was in the map function, because I already had the List for put in the third Parameter of

system.dataset.addColumn(dataset, [colIndex], col, colName, colType)

so that I did put them and everything works

I couldn't see until now.
Thanks :handshake:t3: