Looping Through a PyDataset

Hi, I have a dumb question...
I have this dataset on a custom value property called "test"
image

I would like to retrun only the value "B1" from the only one colum named "parts"
And this is what I have attempted in a Script Transoform...but it only returns the vavlue from row 1, column 0 which is IE100.13

	# Convert to a PyDataset
	pyData = system.dataset.toPyDataSet(value)
	 
	# Use a for loop to extract out a single row at a time
	for row in pyData:
	    # Use either the column index or the column name to extract a single value from that row.
	    val = row[0]
	return val

I honestly do not understand it yet... how would only return the value of row 0, column 0 which is "B1" ?

.
.
.
return pyData[0][0]
1 Like

This will iterate through each row in the dataset, writing val to the row's first column's value each time. Once it ends, val will therefore = the last row's first column's value

1 Like

Thank you very much for the explanation... and how can I adjust this for loop, so that it only returns "B1" value?

Exactly how Kevin answered.

You don't need a loop if you know the index that you want to return.
My question is though, what's the point of the code? You could just as easily (and far more efficiently) return this using an expression binding with:

{this.custom.test}[0,0]
// or possibly just
{this.custom.test}[0]

If you can do something without script, you should always do it that way. Scripting adds an additional layer of load and hence reduces scalability and increases resource requirements on the server.

1 Like

Thank you. this is just for learning purposes... What if I have a 2x2 dataset (row0,row1 x col0, col1)? .. How would I return a value for this two cases:. the value of (row0,col0) and the value of (row1,col1) ? using a the for loop?
I was following this example from the Ignition User Manual:
https://docs.inductiveautomation.com/display/DOC81/Datasets

# Convert to a PyDataset
pyData = system.dataset.toPyDataSet(cities)
 
# Use a for loop to extract out a single row at a time
for row in pyData:
    # Use either the column index or the column name to extract a single value from that row.
    city = row[0]
    population = row["Population"]
    print city, population

Why are you trying to use a loop when you know the address?

You wouldn't knock on the door of every house on the street to see if it was Sid's house if you already knew that Sid was in number 17, would you? You'd just go directly to number 17.

The example you have quoted is performing an action on every row of the dataset. In your question you aren't doing this.

2 Likes

Understood now. Thank you all for the education. Many thanks.

w3schools.com has some very easy to read and use tutorials on lists and dictionaries. I've linked to the start pages. Use the menu on the left of the page to go through the various sections. Getting this clear in your head at the start will make life much easier. (I wish I had!)

Invest some time in working through the exercises.

1 Like

Thank you very much Transistor!