How to add units to every data in a DATASET?
Here I retrieve the dataset from the database. I need to add the 'units' in each data.
Is there anyway that I can round up the decimal points?
Here is the example dataset.
[{"Inve_No":2,"t_stamp":1700616850425,"RealPower":1118.932861328125,"ReactivePower":20.764755249023438,"CB_Status":true,"CB_OpenCmd":false,"Frequency":49.5400505065918,"CB_CloseCmd":false,"DC_Current":25.600000381469727,"DC_Power":450.29998779296875}]
When you get it and edit it, will it actually be a dataset datatype, or will it be in the json format you've posted?
With scripting on a dataset datatype, it can be done like this:
# originalDataset = Get the original dataset
# Get the headers from the dataset
headers = system.dataset.getColumnHeaders(originalDataset)
# Create a list for the the processed rows
newData = []
# Iterate through every row of the original dataset
for row in range(originalDataset.rowCount):
# Create a list for the processed column values of each row
newRow = []
# Iterate through each column of each row, and get the values
for column in range(originalDataset.columnCount):
value = originalDataset.getValueAt(row, column)
# If a value is a float, round it and apend it to the new row. Otherwise, just append it to the new row
# ...The ", 0" in the round function means round to zero decimal places. This can be adjusted as needed.
if isinstance(value, float):
newRow.append(round(value, 0))
else:
newRow.append(value)
# Append each fully processed row to the new data list
newData.append(newRow)
# Convert the new data list to a dataset using the headers from the original dataset
newDataset = system.dataset.toDataSet(headers, newData)
Found the solution
units=['',' MW',' MVar','','',' Hz','',' A',' W'] # units for data
B = newdata.getColumnAsList(0) # select column data which need to add units
baz = [x+y for x, y in zip(B, units)] # Create new column adding units
for i in range (9):
newdata = system.dataset.updateRow(newdata, i, {"Value 1": baz[i]}) #update the each row with the added units to the column data