# Initializing a dataset

**URL:** <https://forum.inductiveautomation.com/t/initializing-a-dataset/5948>\
**Category:** Ignition\
**Created:** [March 1, 2013, 2:35pm UTC](https://forum.inductiveautomation.com/t/initializing-a-dataset/5948 "2013-03-01T14:35:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnguyen](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@gnguyen](https://forum.inductiveautomation.com/u/gnguyen)\
**Post date:** [March 1, 2013, 2:35pm UTC](https://forum.inductiveautomation.com/t/initializing-a-dataset/5948/1 "2013-03-01T14:35:09Z")

</div>

Hi,

I need to pivot a table and to do so, create a dataset with system.dataset.toDataSet(header, row).  
The problem is that all the columns are in string type instead of int or double, and then the report cannot make any calculation on it.

I cannot initialize statically the header and the first row as the length is variable ; also, when I try to initialize the first row in the same loop as for the header, I get “IndexError: Row 0 doesn’t have the same number of columns as header list”…

Any idea ?

[code]DS = event.source.parent.getComponent(‘Table 1’).data  
pODS = system.dataset.toPyDataSet(DS) #old pydataset  
header = []  
row = []  
nbCol = DS.getColumnCount()  
nbRow = DS.getRowCount()

#header  
header.append(“Unité”)  
for i in range (nbRow):  
header.append(app.char.sans\_accents(system.db.dateFormat(pODS[i][“t\_stamp”],“MMM”)).upper())

#creation du new dataset  
NDS = system.dataset.toDataSet(header, row)

#rows  
for j in range(1,nbCol-1):  
row.append(DS.getColumnName(j))  
for i in range (nbRow):  
row.append(int(pODS[i][j])\*2)  
NDS = system.dataset.addRow(NDS, row)  
row = []

event.source.parent.getComponent(‘Table 2’).data = NDS[/code]

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [March 1, 2013, 4:08pm UTC](https://forum.inductiveautomation.com/t/initializing-a-dataset/5948/2 "2013-03-01T16:08:54Z")

</div>

I would create the header row, then the data row, then create the data set.

[code]DS = event.source.parent.getComponent(‘Table 1’).data  
pODS = system.dataset.toPyDataSet(DS) #old pydataset  
header = []  
rows = []  
nbCol = DS.getColumnCount()  
nbRow = DS.getRowCount()

#header  
header.append(“Unité”)  
for i in range (nbRow):  
header.append(app.char.sans\_accents(system.db.dateFormat(pODS[i][“t\_stamp”],“MMM”)).upper())

#rows  
for j in range(1,nbCol-1):  
row = []  
row.append(DS.getColumnName(j))  
for i in range (nbRow):  
row.append(int(pODS[i][j])\*2)  
rows.append(row)

#creation du new dataset  
NDS = system.dataset.toDataSet(header, rows)

event.source.parent.getComponent(‘Table 2’).data = NDS[/code]

There is some magic that happens behind the scenes when the first data row is added to a dataset that determines what datatype each column should be. By creating a row with data in it first, it has a chance to guess correctly.

---

<div class="post-metadata">

**Author:** ![gnguyen](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@gnguyen](https://forum.inductiveautomation.com/u/gnguyen)\
**Post date:** [March 1, 2013, 4:20pm UTC](https://forum.inductiveautomation.com/t/initializing-a-dataset/5948/3 "2013-03-01T16:20:59Z")

</div>

😃😃  
Work nice.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [March 1, 2013, 11:43pm UTC](https://forum.inductiveautomation.com/t/initializing-a-dataset/5948/4 "2013-03-01T23:43:23Z")

</div>

Your welcome ![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:")

I’ve run into a smiler problem in the past where the different data rows rows held different data. In was fine in testing. The first row in production had a number in that column instead of a string so it complained on the second row.

Experience: what you get just after you need it 🙂
