Filter Dataset Based on Column Value

There's lots of ways to skin this cat, but if you truly want to do it from scripting, it'll probably be easiest if you convert to a PyDataset first.
If all your conditions are simple literal values, you can do something like this:

def filterDataset(ds, **kwargs):
	pds = system.dataset.toPyDataSet(ds)
	output = []
	for row in pds:
		if all(row[key] == value for key, value in kwargs.items()):
			output.append(row)
	
	return system.dataset.toDataSet(list(ds.columnNames), output)

E.G:

def sampleData(rows=10):
	from random import choice, randint
	
	machines = [
		"DSHW0001",
		"DSHW0002",
		"DSHW0003",
		"FGHW0001",
		"FGHW0002",
		"FGHW0003",
		"MCHW0001",
		"MCHW0002",
		"MCHW0003",
	]
	activityTypes = [
		"ISSUE",
		"PROD",
	]
	data = [
		[choice(machines), choice(activityTypes), system.date.now(), "ABC{:05}".format(randint(0, 100))]
		for _ in xrange(rows)
	]
	
	headers = ["machineName", "activityType", "productionDate", "orderNumber"]
	return system.dataset.toDataSet(headers, data)

ds = sampleData()
print "input:"
system.dataset.print(ds)

def filterDataset(ds, **kwargs):
	pds = system.dataset.toPyDataSet(ds)
	output = []
	for row in pds:
		if all(row[key] == value for key, value in kwargs.items()):
			output.append(row)
	
	return system.dataset.toDataSet(list(ds.columnNames), output)
	

filtered = filterDataset(ds, machineName="DSHW0001", activityType="ISSUE")
print "filtered:"
system.dataset.print(filtered)
input:
| Row | machineName | activityType |               productionDate | orderNumber |
| --- | ----------- | ------------ | ---------------------------- | ----------- |
|   0 |    FGHW0001 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00054 |
|   1 |    MCHW0001 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00073 |
|   2 |    FGHW0002 |         PROD | Tue Oct 18 20:35:23 UTC 2022 |    ABC00096 |
|   3 |    FGHW0003 |         PROD | Tue Oct 18 20:35:23 UTC 2022 |    ABC00053 |
|   4 |    DSHW0001 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00067 |
|   5 |    FGHW0002 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00021 |
|   6 |    MCHW0001 |         PROD | Tue Oct 18 20:35:23 UTC 2022 |    ABC00002 |
|   7 |    MCHW0001 |         PROD | Tue Oct 18 20:35:23 UTC 2022 |    ABC00059 |
|   8 |    FGHW0002 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00094 |
|   9 |    FGHW0001 |         PROD | Tue Oct 18 20:35:23 UTC 2022 |    ABC00071 |
filtered:
| Row | machineName | activityType |               productionDate | orderNumber |
| --- | ----------- | ------------ | ---------------------------- | ----------- |
|   0 |    DSHW0001 |        ISSUE | Tue Oct 18 20:35:23 UTC 2022 |    ABC00067 |

system.dataset.print comes from Ignition Extensions but is just useful for testing, not necessary.

2 Likes