How can I merge columns part way through a dataset with an append at the end of each shift in a dataset?
Should I make a new post for this one?
I know how to create a row using append.
I am not sure how create a row that has a different number of columns though.
Input:
| Line | Shift | Hours | Gross | Net | Timestamp |
| K01 | 1 | 12 | 200 | 150 | 2022-09-25 07:00:00 |
| K02 | 1 | 12 | 202 | 152 | 2022-09-25 07:00:00 |
| K03 | 1 | 12 | 203 | 153 | 2022-09-25 07:00:00 |
| K01 | 1 | 12 | 200 | 150 | 2022-09-25 15:00:00 |
| K02 | 1 | 12 | 202 | 152 | 2022-09-25 15:00:00 |
| K03 | 1 | 12 | 203 | 153 | 2022-09-25 15:00:00 |
Output
| Line | Shift | Hours | Gross | Net | Timestamp |
| K01 | 1 | 12 | 200 | 150 | 2022-09-25 07:00:00 |
| K02 | 1 | 12 | 202 | 152 | 2022-09-25 07:00:00 |
| K03 | 1 | 12 | 203 | 153 | 2022-09-25 07:00:00 |
| Shift 1 totals here | 605 | 455 | 2022-09-25 07:00:00 |
| K01 | 1 | 12 | 300 | 150 | 2022-09-25 15:00:00 |
| K02 | 1 | 12 | 302 | 152 | 2022-09-25 15:00:00 |
| K03 | 1 | 12 | 303 | 153 | 2022-09-25 15:00:00 |
| Shift 2 totals here | 905 | 455 | 2022-09-25 07:00:00 |
or
output 2
| Line | Shift | Hours | Gross | Net | Timestamp |
| K01 | 1 | 12 | 200 | 150 | 2022-09-25 07:00:00 |
| K02 | 1 | 12 | 202 | 152 | 2022-09-25 07:00:00 |
| K03 | 1 | 12 | 203 | 153 | 2022-09-25 07:00:00 |
| Shift 1 totals here | 605 | 455 |
| K01 | 1 | 12 | 300 | 150 | 2022-09-25 15:00:00 |
| K02 | 1 | 12 | 302 | 152 | 2022-09-25 15:00:00 |
| K03 | 1 | 12 | 303 | 153 | 2022-09-25 15:00:00 |
| Shift 2 totals here | 905 | 455 |
My closest attempt, I thought I might find a way to get an index of the row where the shift become 2.
I haven’t figured out how to get that index of that row where the shift is 2 though.
Probably some way to look for an index where a column name is something, and index will take the first one I thought. Here is my most recent try including generating a list and generating a single total:
### Sample Dataset to simulate query result
import random
headers = ['date', 'shift', 'line', 'hours', 'gross', 'net']
data = []
t_stamp = system.date.now()
for i in range(10):
t_stamp = system.date.addMinutes(t_stamp, 1)
date=t_stamp
if i<4:
shift=1
else:
shift=2
lineCol=('b' +str(random.randrange(5)))
hoursCol=random.randrange(12)
gross= random.randrange(100)
net=random.randrange(100)
data.append([date,shift, lineCol,hoursCol,gross,net])
dataIn = system.dataset.toPyDataSet(system.dataset.toDataSet(headers, data))
#for row in dataIn:
# for value in row:
# print value
for row in dataIn:
print [row[colName] for colName in dataIn.getColumnNames()]
print '---'
dataOut=[]
#for row in range(dataIn.getRowCount()):
test=sum( dataIn.getValueAt(row,dataIn.getColumnIndex('net')) for row in range(dataIn.getRowCount()))
print test
def handleDivideZero(a,b):
return 0 if (a and b)==0 else a/b
print data
data.append([t_stamp,0,' '+' testing this',0,0,test])
print data
#dataOut= system system.dataset.toPyDataSet(system.dataset.toDataSet(headers, data))
index = (row.getValueAt('shift') for row in dataIn)
print index