# Py DataSet looping and appending trouble (repeating first line)

**URL:** <https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 20, 2024, 9:11pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639 "2024-05-20T21:11:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Bosch](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Thomas\_Bosch](https://forum.inductiveautomation.com/u/Thomas_Bosch)\
**Post date:** [May 20, 2024, 9:11pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/1 "2024-05-20T21:11:13Z")

</div>

Hello everyone,

I'm trying to create a new dataset line by line based off of data in an existing dataset so I can add some calculations based on existing data for a report I'm generating. I've gotten to the point where it is doing the calculations and appending the row to my py Data Set, but for some reason it is making every row the data / calcs from the first row of the existing dataset instead of looping through. It does end up with the right amount of rows... but they're all the same. Here's the code I've cobbled together so far:

```python
def updateData(data, sample):
	#Import SQL query results
	dataset = data['TestQuery'].getCoreResults()
	
	#Convert to PyDataset
	pyData = system.dataset.toPyDataSet(dataset)
	
	#Import L/360 Value
	L360 = data['L360']
	
	#Create dataset for new data with calcs
	pyDataCalcs = []
	
	header = ['CheckID', 'Product', 'RatedLoad', 'TestResults', 'Slope', 'SlopeL360']
	
	#Loop for performing cacls and adding each row to pyDataCals
	for row in pyData:
		#Set Values for Original Columns
		CheckID = row[0]
		Product = row[1]
		TestResults = row[2]

		
		#Set Value of Rated Load (16 for heavy duty products or 12 for medium duty)
		if Product == "Product1":
			RatedLoad = 16
		elif Product == "Product2":
			RatedLoad = 16
		else:
			RatedLoad = 12
		
		#Perform slope calcs based on rated load
		Slope = RatedLoad / TestResults
		SlopeL360 = Slope * L360
		
		newRow = [CheckID, Product, RatedLoad, TestResults, Slope, SlopeL360]
		pyDataCalcs.append(newRow)

		
	dataSetCalcs = system.dataset.toDataSet(header, pyDataCalcs)
	
	data['FullData'] = dataSetCalcs

```

Any help would be greatly appreciated, I'm assuming I'm not fully comprehending how either append works or the looping function.

---

<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:** [May 20, 2024, 9:17pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/2 "2024-05-20T21:17:12Z")

</div>

I don't see any obvious problem. All whitespace/indentation is appropriate.

Consider using a logger to push each `newRow` into your logs at runtime as a sanity check.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 20, 2024, 9:27pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/3 "2024-05-20T21:27:31Z")

</div>

By any chance are you expecting two integer values to divide into a float?

In python 2.7 you have to be explicit and force it to do float division, otherwise you’ll end up with truncated values, that may ‘seem’ correct but aren’t.

---

<div class="post-metadata">

**Author:** ![Thomas\_Bosch](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Thomas\_Bosch](https://forum.inductiveautomation.com/u/Thomas_Bosch)\
**Post date:** [May 20, 2024, 10:25pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/4 "2024-05-20T22:25:18Z")

</div>

Not sure if the script would automatically make "RatedLoad" an integer since it's doing that within the script, but I know TestResults is not an integer.

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [May 20, 2024, 10:34pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/5 "2024-05-20T22:34:00Z")

</div>

> [@Thomas\_Bosch](#):
>
> `Slope = RatedLoad / TestResults`

To be safe, I would write:

`Slope = float(RatedLoad) / float(TestResults)`

---

<div class="post-metadata">

**Author:** ![Ujwal\_Wankhede](https://avatars.discourse-cdn.com/v4/letter/u/4af34b/32.png) [@Ujwal\_Wankhede](https://forum.inductiveautomation.com/u/Ujwal_Wankhede)\
**Post date:** [May 21, 2024, 1:56am UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/6 "2024-05-21T01:56:04Z")

</div>

> [@Thomas\_Bosch](#):
>
> a report I'm generating

It seems like there might be a mix-up with the data keys in your table configuration. It looks like you've assigned the existing dataset to the Data Key for the table, but each cell is referencing a key from data['FullData']. This can cause the table to display the data from the first row of the dataset in each row of the table.  
_Following screenshot reference is to recreate issue and produce wrong result._

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/f/0f00b236476bfac17b91353dad2280314596e176.png)

---

<div class="post-metadata">

**Author:** ![Thomas\_Bosch](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Thomas\_Bosch](https://forum.inductiveautomation.com/u/Thomas_Bosch)\
**Post date:** [May 21, 2024, 3:14pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/7 "2024-05-21T15:14:23Z")

</div>

Thanks, I've updated the script. Unfortunately no change, but good to know good practices.

---

<div class="post-metadata">

**Author:** ![Thomas\_Bosch](https://avatars.discourse-cdn.com/v4/letter/t/2acd7d/32.png) [@Thomas\_Bosch](https://forum.inductiveautomation.com/u/Thomas_Bosch)\
**Post date:** [May 21, 2024, 3:26pm UTC](https://forum.inductiveautomation.com/t/py-dataset-looping-and-appending-trouble-repeating-first-line/90639/8 "2024-05-21T15:26:46Z")

</div>

That was it, I copy pasted the table to do a comparison and tried to just replace the columns and not the table Data Key. Thank you so much, I wasn't even looking in the right place!
