Py DataSet looping and appending trouble (repeating first line)

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:

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.

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.

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.

1 Like

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.

To be safe, I would write:

Slope = float(RatedLoad) / float(TestResults)

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.

1 Like

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

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!