Removing Rows from a Table

Hey all,

I’m trying to delete rows from a data table depending on what .csv file I imported and i’m having trouble creating a loop which reads each row as a conditional statement.

As you can see I’m trying to compare each value in the last column (“ID”) to 3 and remove the entire row if it does not equal 3. I can get it to work if I call out individual cells in the table but since the .csv files can vary in length I need something more like a loop. The .csv upload piece of the code works perfectly but i’m not exactly sure what I need to do to create that conditional loop the .getValueAt command is not working.

I believe you need to use a FOR loop to loop the data in the table.

py_data = system.dataset.toPyDataSet(

for row in py_data:
      if event.source.parent.getValueAt([], 'ID') != 3:

I’m not sure everything needed to execute this code is there, but it should be a good start

1 Like
import csv
path = system.file.openFile("csv")
data = csv.reader(open(path))
header =
datasetData = []
for row in data:
	if row[-1] == 3:
		datasetData.append(row) = system.dataset.toDataSet(headers, datasetData)

Rather than selectively delete rows after reading the entire file, just skip those rows when constructing the dataset. row[-1] will refer to the last element in the sequence of columns on that row, so even if the file format changes, it will always check the last column.


Both of you are life savers thank you so much!

@PGriffith So I have spent about a day struggling to get this to work and python/ignition is still throwing me a executing script error.

import csv
path = system.file.openFile("csv")
csvData = csv.reader(open(path))
header =

datasetData = []
for row in csvData:
	if row[-1] == 3:
		datasetData.append(row) = system.dataset.toDataSet(header, datasetData)
#since I'm using just a standard button and Table 
event.source.parent.getComponent('Table').data =

I have also tried @dkhayes method with no luck either. I attached the .csv file that ive been practicing with at the bottom.

Also since row[-1] is suppose to analyze the last cell of a row, how would one go about analyzing intermediate columns.

Dispatch dummy.csv (133 Bytes)

Try this; I think a DictReader is more appropriate if you might be getting elements in different orders.

data = ["Sample,Location,Latitude,longitude,ID",

import csv
csvData = csv.DictReader(data)
headers = [h.capitalize() for h in] #just goes through the headers to format them the same

datasetData = []
for row in csvData:
	if row['ID'] == '3': #all items are read as strings, so either cast row['ID'] to an integer, or do a string<->string comparison

ds = system.dataset.toDataSet(headers, datasetData)
print ds
print ds.getColumnNames()

The OP may be referring more to slicing (tutorial here):

@agweb.salinas: In your current code, ‘table’ in line 11 is not defined. That’s where your error is happening.

The file you posted has a Byte Order Marker (BOM) at the beginning of the file. I don’t know if that’s intentional, or just an artifact of making a csv file in Excel or whatever you used to make it. If it normally doesn’t, then don’t worry about it, But to make the results consistent, the file should be opened using the codecs library.

import csv, codecs

#path = 'C:\\Test\\Dispatch Dummy.csv'

path = system.file.openFile("csv")
if path != None:
  csvData = csv.reader(, encoding='utf-8-sig'))

  header = [h.capitalize() for h in]
  datasetData = []

  for row in csvData:
    if row[-1] == '3':
dataOut = system.dataset.toDataSet(header, datasetData)
event.source.parent.getComponent('Table').data = dataOut