# Filter Dataset Based on Column Value

**URL:** <https://forum.inductiveautomation.com/t/filter-dataset-based-on-column-value/65356>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [October 18, 2022, 7:09pm UTC](https://forum.inductiveautomation.com/t/filter-dataset-based-on-column-value/65356 "2022-10-18T19:09:29Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 18, 2022, 8:38pm UTC](https://forum.inductiveautomation.com/t/filter-dataset-based-on-column-value/65356/2 "2022-10-18T20:38:23Z")

</div>

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:

```python
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:

```python
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)

```

```auto
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](https://forum.inductiveautomation.com/t/ignition-extensions-convenience-utilities-for-advanced-users/64615) but is just useful for testing, not necessary.

---

_[View the full topic](https://forum.inductiveautomation.com/t/filter-dataset-based-on-column-value/65356)._
