Delete value on the tag dataset

if I want to remove the number 20 for example in the dataset of type 4x1

NameColumn
30
20
12
10

I use this line of code:
dsDevice = self.view.params.listZoneDesservies # dataset
dsDevice = system.dataset.deleteRow(dsDevice,self.view.params.idZone)
The problem is that I know the value I want to delete but not its index.
In the dataset, each value will be unique. so if I can delete a value without knowing its index, that would be good

What function should I use to remove the value without knowing its index in the dataset?

Hello. Iterate through the dataset with a loop and use the .getValueAt function. That should do the trick.

2 Likes

Many possible ways to do this; here are a few:

def filterDataset(dataset, valueToExclude, column=0):
	rowsToRemove = []
	for row in xrange(dataset.rowCount):
		if dataset.getValueAt(row, column) == valueToExclude:
			rowsToRemove.append(row)

	return system.dataset.deleteRows(dataset, rowsToRemove)

def filterDataset(dataset, valueToExclude, column=0):
	pds = system.dataset.toPyDataSet(dataset)

	rowsToRemove = []
	for index, row in enumerate(pds):
		if row[column] == valueToExclude:
			rowsToRemove.append(index)

	return system.dataset.deleteRows(dataset, rowsToRemove)

def filterDataset(dataset, valueToExclude, column=0):
	pds = system.dataset.toPyDataSet(dataset)

	data = []
	for index, row in enumerate(pds):
		if row[column] != valueToExclude:
			data.append(list(row))

	return system.dataset.toDataSet(list(dataset.columnNames), data)

def filterDataset(dataset, valueToExclude, column=0):
	# requires Ignition Extensions: https://forum.inductiveautomation.com/t/ignition-extensions-convenience-utilities-for-advanced-users/64615
	def filterFn(**columns):
		return columns[column] != valueToExclude

	return system.dataset.filter(dataset, filterFn)
1 Like

I would do this:

def filterDataset(ds, exclude, column=0):
	try:
		idx = int(column)
	except:
		idx = ds.getColumnIndex(column)
	values = ds.getColumnAsList(idx)
	rowExcludes = [i for (i, v) in enumerate(values) if v == exclude]
	if rowExcludes:
		return system.dataset.deleteRows(ds, rowExcludes)
	return ds

Accepts a column index or a column name. Returns the original dataset if no rows matched.

5 Likes

here is the script i made to delete one value, it works.
first I check if the value I want to delete exists in the dataset

dsZone = self.view.params.DeviceZone #dataset
if self.view.params.idDevice in dsZone.getColumnAsList(0):
			for i in range(dsZone.getRowCount()):
			   if dsZone.getValueAt(i, 'listDevice') == self.view.params.idDevice:	
					dsZone = system.dataset.deleteRow(dsZone,i)
					system.tag.writeBlocking([zonePath],[dsZone])
					break

Why ? You're already doing that with the inner if.
And if you're converting the column to a list, might as well use that to find the index instead of going through your dataset in a loop...

Use one of the scripts above. They have the added advantage of being reusable.

If you want something closer to your own code:

dsZone = self.view.params.DeviceZone
index = dsZone.getColumnAsList(0).index(self.view.params.idDevice)
dsZone = system.dataset.deleteRow(dsZone, index)
system.tag.writeBlocking([zonePath], [dsZone])

But again, do you NEED a dataset tag for just one column ? It's been several threads were you're doing list operation on that same one column dataset, and it seems to me you could make your life simpler by using an array instead...

1 Like

dsZone is a dataset that I get from a tag.

Yes I'am doing operation on that same one column dataset because the person who created the dataset type tag did not use the array. This is the reason why I'am using same type of data.

it's true that it would be easier to use array.

And I nedd delete one row not a list of value from dataset reason why I'am using deleteRow and not deleteRows

You can change the data type of a tag with just a few clicks.

deleteRows(ds, [i]) will do the same thing as deleteRows(ds, i).
I wouldn't be surprised if they're just wrappers around the exact same function.

The problem this dataset is used on other script. If I change dataset as Array, I think it will cause problem. I will change to later.

You keep harping on using an array instead of a dataset. Unless you are using Perspective, and this topic is agnostic on that point (because the algorithms apply anywhere), datasets are much more platform-friendly. In particular, Vision doesn't have a client-side custom property type for arrays.

Please confine your array advice to situations where it is known that the data is only going to be used in Perspective.

I'll keep that in mind, sorry.

But I'm pretty sure we've established in a previous thread that he's using perspective.

I'am using perspective