# Combine Dataset Values based on Column Value

**URL:** <https://forum.inductiveautomation.com/t/combine-dataset-values-based-on-column-value/43180>\
**Category:** Ignition\
**Created:** [February 9, 2021, 3:01pm UTC](https://forum.inductiveautomation.com/t/combine-dataset-values-based-on-column-value/43180 "2021-02-09T15:01:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [February 9, 2021, 3:01pm UTC](https://forum.inductiveautomation.com/t/combine-dataset-values-based-on-column-value/43180/1 "2021-02-09T15:01:59Z")

</div>

@JordanCClark  
Do you have any magic to combine these two rows based on that row 1 has `2` in the `ITEM` column? I can do it with some ugly nested for loops, and I’m not good with list comprehensions, didn’t know if my script is the best way. Thanks!

before

```auto
row | STYLE | ACKNO | ACKLN | ITEM | FAB | WELT | COUNT | TOTAL | CLASS | PATTERN
-------------------------------------------------------------------------------------------------
0 | 990530 | 640403 | 60 | A-PILL 1 | OAK ISLAND 23 | | 2.0 | 2.0 | A | Y      
1 | 990530 | 640403 | 60 | A-PILL 2 | BLUECOVE 22 | | 2.0 | 2.0 | B | Y    

```

should look like this afterwards

```auto
row | STYLE | ACKNO | ACKLN | ITEM | FAB | WELT | COUNT | TOTAL | CLASS | PATTERN
-------------------------------------------------------------------------------------------------
0 | 990530 | 640403 | 60 | A-PILL 1 | OAK ISLAND 23/BLUECOVE 22 | | 2.0 | 2.0 | A | Y          

```

My current script

```auto
for i,row in enumerate(ds2):
	item = row['ITEM']
	if '2' in item:
		fab2 = row['FAB']
		for j,row in enumerate(ds2):
			if row['ITEM'] == item.replace('2','1'):
				newFab = row['FAB'] + '/' + fab2
				newData = system.dataset.setValue(ds2, j,'FAB',newFab)
				newData = system.dataset.deleteRow(newData, i)

```

---

<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:** [February 9, 2021, 4:20pm UTC](https://forum.inductiveautomation.com/t/combine-dataset-values-based-on-column-value/43180/2 "2021-02-09T16:20:37Z")

</div>

2 cents: Your way looks fine. It’s a hard problem to ‘generalize’, and the best script is the one you remember how it works in six months/five years 🙂.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 9, 2021, 4:46pm UTC](https://forum.inductiveautomation.com/t/combine-dataset-values-based-on-column-value/43180/3 "2021-02-09T16:46:48Z")

</div>

+1 to Paul’s statement. But, since you asked… 😆

Converting to a PyDataSet will let you use subscripts, and grab an entire row as a list. We can then modify that list directly.

```python
# Convert to PyDataSet
pyDS = system.dataset.toPyDataSet(ds2)

# Get the column names
headers = list(pyDS.getColumnNames())

# Get the index of the column we want to concatenate
idx = headers.index('FAB')

# Get the first row of the PyDataSet
dataOut = list(pyDS[0])

# Join the column values
dataOut[idx] = '/'.join(pyDS.getColumnAsList(idx))

# Make a new Dataset
newDS = system.dataset.toDataSet(headers, [dataOut])

```
