# How to return a row and column of specific data from a dataset

**URL:** <https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101>\
**Category:** Ignition\
**Created:** [June 23, 2021, 12:18pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101 "2021-06-23T12:18:03Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 23, 2023, 3:33pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/22 "2023-08-23T15:33:33Z")

</div>

The whole script seems... kinda wrong.  
the `findEquals` function takes a dataset as parameter, but it's never used.  
Instead, you use the `session` variable defined out of it.  
You're importing `system` twice, including right in the middle of the script, AFTER system has already been used. And you shouldn't be importing it at all  
You're also import `System` from `java.lang` and not using it.

Let me start a sandbox and try to make it prettier

---

<div class="post-metadata">

**Author:** ![William\_R](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/william_r/32/32710_2.png) [@William\_R](https://forum.inductiveautomation.com/u/William_R)\
**Post date:** [August 23, 2023, 3:33pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/23 "2023-08-23T15:33:44Z")

</div>

Thanks for your reply. I have never used the script library, and didn't even know it existed tbh. Is it bad to write scripts in the gateway? Likewise, why is it bad to import system? I saw that it was needed by some dev posts to use the system library in the gateway. Is this bad practice?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 23, 2023, 3:34pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/24 "2023-08-23T15:34:20Z")

</div>

> [@William\_R](#):
>
> its working now

Until the next edit.

Follow pascal's advice. In particular, never use `def` directly in a gateway event. Use a project library script.

---

<div class="post-metadata">

**Author:** ![William\_R](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/william_r/32/32710_2.png) [@William\_R](https://forum.inductiveautomation.com/u/William_R)\
**Post date:** [August 23, 2023, 3:35pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/25 "2023-08-23T15:35:18Z")

</div>

Will do. Thanks for helping me work through the beginner's bad habits.

---

<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:** [August 23, 2023, 3:44pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/26 "2023-08-23T15:44:08Z")

</div>

You don't need to `import system` **except** in the special case of a defined function in a gateway event script, because they use legacy (insane) Python 2.5 or 2.1 or something scoping rules. **Everywhere else** , things work consistently, it's just bad luck you happened upon this.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 23, 2023, 4:05pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/27 "2023-08-23T16:05:54Z")

</div>

Alright so I think this should work. 2 versions, one that's closer to what you were doing, using a function to filter rows then counting the number of rows in the filtered results (it's always useful to have a filter function anyway),  
and another one that's more... direct

v1:  
in the script library (note that I have filterrows in a `ds_utils` script, which is why the function call is `ds_utils.filter_rows(...)`. Put things wherever you want and name them whatever you want, but adapt the paths/names accordingly)

```python
def filter_rows(ds, predicate):
	return system.dataset.toDataSet(
		list(ds.columnNames),
		[row for row in system.dataset.toPyDataSet(ds) if predicate(row)]
	)

def lockout_user_role():
	sessions = system.util.getSessionInfo()
	filtered_ds = ds_utils.filter_rows(sessions, lambda row: row['username'] == "Lockout" and row['address'] == "DESKTOP-XXXXXX")

	if filtered_ds.rowCount > 0:
		system.tag.writeBlocking(['[default]LockoutUserRole'], True)

```

in the gateway event:

```python
your_script_path.lockout_user_role()

```

v2:

```python
def lockout_user_role():
	session = system.dataset.toPyDataSet(system.util.getSessionInfo())
	if any(row['username'] == "Lockout" and row['address'] == "DESKTOP-XXXXXX" for row in session):
		system.tag.writeBlocking(['[default]LockoutUserRole'], True)

```

edit: That's assuming I understood the requirements :X

---

<div class="post-metadata">

**Author:** ![William\_R](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/william_r/32/32710_2.png) [@William\_R](https://forum.inductiveautomation.com/u/William_R)\
**Post date:** [August 23, 2023, 4:34pm UTC](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101/28 "2023-08-23T16:34:26Z")

</div>

Wow. Very eye opening. I feel like I unlocked a new level doing this task and learning from you guys. This code makes MUCH more sense. Thanks for taking the time to work that out and show me. I will be implementing this, and using project libraries from now on.

[Previous page](https://forum.inductiveautomation.com/t/how-to-return-a-row-and-column-of-specific-data-from-a-dataset/48101.md?page=1)
