I am trying to loop through a table and remove any row that has a column with a specific value. See sample code below. This is not working, what should i be doing and is there a better way? I do not have access to the stored procedures to filter in the query.
table = event.source.parent.getComponent(“table”)
data = system.dataset.toPyDataSet(table.data)
newData = system.dataset()
for row in data:
if row[“ColName”] == “value”:
newData = system.dataset.deleteRow(data, row)
“lambda” is Python syntax for creating an anonymous function. The “findRows” method finds the rows you want to delete by evaluating the lambda function for each row.
Edit – A new version of the PA Power Scripting Module was just released. A function can now be passed directly to the removeRows method, like this:
table = event.source.parent.getComponent('Table')
data = pa.dataset.toData(table.data)
for row in data.copy():
if row["ColName"] == "value":
data.removeRow(row)
table.data = data.toDataSet()
Hi Jerry,
Your code will only delete the last row it finds, and if it finds none, return garbage. Also, deleting a row changes the following row numbers, complicating the loop. The best approach is to find all the row numbers you want to delete, then delete them all at once.table = event.source.parent.getComponent("table")
c = table.getColumnIndex("ColName")
unwanted = []
for r in range(table.rowCount):
if table.getValueAt(r, c)=="value":
unwanted.append(r)
newData = system.dataset.deleteRows(table, unwanted)
Thanks pturmel! Had to make a small modification to the code, needed table.data. Below is what worked for me:
table = event.source.parent.getComponent('table')
data = table.data
unwanted = []
for r in range(data.rowCount):
if data.getValueAt(r, "column")=="02":
unwanted.append(r)
newData = system.dataset.deleteRows(data, unwanted)
table.data = newData