Combine two datasets

You will need to iterate through each dataset. I find dictionaries useful for this.

# Set up sample datasets
data1 = [['t_stamp', 'val1'],
         [1,  123],
		 [3,  456],
		 [4,  789],
		 [9,  345],
		 [10, 678],
		 [11, 901],
		 [15, 234],
		 [17, 567],
		 [20, 890],
		]
data2 = [['t_stamp', 'val2'],
         [2,  987],
		 [3,  654],
		 [7,  321],
		 [9,  876],
		 [12, 543],
		 [14, 210],
		 [17, 765],
		 [20, 432],
		 [22, 109],
		]

dataset1 = system.dataset.toDataSet(data1[0], data1[1:])
dataset2 = system.dataset.toDataSet(data2[0], data2[1:])

########################################################

def combineDatasets(dataList, commonCol = 't_stamp'):
	''' Combine multiple datasets based on a common column
		dataList: list of datasets
		commonCol: column name common to all datasets	    
	'''
	# Convert all datsets to BasicDataset, if needed
	for i, data in enumerate(dataList):
		if 'com.inductiveautomation.ignition.common.BasicDataset' not in str(type(data)):
			dataList[i] = system.dataset.toDataSet(data)
	
	# Create default value dictionary containing all column names
	# with None values
	blankValueDict = {}
	for data in dataList:
		colNames = list(data.getColumnNames())
		for col in colNames:
			if col != commonCol and col not in blankValueDict.keys():
				blankValueDict[col] = None

	# Process the data
	dataDict = {}
	for data in dataList:
		colNames = list(data.getColumnNames())
		for i in xrange(data.rowCount):
			commonColValue = data.getValueAt(i, commonCol)
			if commonColValue not in dataDict.keys():
				dataDict[commonColValue] = blankValueDict.copy()
			for col in colNames:
				if col != commonCol:
					dataDict[commonColValue][col] = data.getValueAt(i, col)

	# Create combined dataset
	headers = [commonCol] + sorted(blankValueDict.keys())
	data = []
	for key in sorted(dataDict.keys()):
		newRow=[]
		newRow.append(key)
		for col in headers[1:]:
			newRow.append(dataDict[key][col])
		data.append(newRow)
	
	return system.dataset.toDataSet(headers, data)

combinedData = combineDatasets([dataset1, dataset2])

Output:

row | t_stamp | val1 | val2
---------------------------
0   | 1       | 123  | None
1   | 2       | None | 987 
2   | 3       | 456  | 654 
3   | 4       | 789  | None
4   | 7       | None | 321 
5   | 9       | 345  | 876 
6   | 10      | 678  | None
7   | 11      | 901  | None
8   | 12      | None | 543 
9   | 14      | None | 210 
10  | 15      | 234  | None
11  | 17      | 567  | 765 
12  | 20      | 890  | 432 
13  | 22      | None | 109 
2 Likes