Rename the column of a dataset

Hi,
I have dataset [61R×4C]

I want to rename the column 4 name . How to do that using script?

@JordanCClark any idea to do?

Depends on where the dataset is coming from. The easiest way is to change it at the source.

If you really want to do this in script then you would have to rebuild the Dataset

ds = *Your Data*
headers = system.dataset.getColumnHeaders()
headers[3] = "New Column Name"
values = [[ds.getValueAt(row,col) for col in range(ds.columnCount)] for row in range(ds.rowCount)]
ds = system.dataset.toDataSet(headers,values)
1 Like

Thanks i will try it out

Check edit, modified the script, because you need a list of lists.

More Coffee, needed apparently.

1 Like

Please don’t ping users looking for help unless it’s related to something they have already answered. That’s not fair to them unless you’re going to pay for their work.


Set your query to return Document format.
Add a script transform and modify the following code. (You can test it in the Script Console.

# Example of `value` returned by database query.
value = [
  {"city": "Helsinki", "country": "Finland", "population": 635591},
  {"city": "Jakarta", "country": "Indonesia", "population": 10187595},
  {"city": "Madrid", "country": "Spain", "population": 3233527}
]

for row in range(len(value)):
	# Create a new dictionary item equal to the old one 
	# while popping the old one out of the dictionary.
	value[row]['capital'] = value[row].pop('city')

print value     # Use this in the Script Console and delete in the Transform.
# return value	# Uncomment this in the Script Transform.

This returns

[
  {'country': 'Finland', 'capital': 'Helsinki', 'population': 635591}, 
  {'country': 'Indonesia', 'capital': 'Jakarta', 'population': 10187595}, 
  {'country': 'Spain', 'capital': 'Madrid', 'population': 3233527}
]

In general you shouldn’t have to do this. If the data is filling a table or chart you can specify the heading or caption on the actual component.

3 Likes

Assuming this is for perspective and a query binding is being used.

Changing the query output would not be available in vision, as far as I’m aware.

Good point, Irose. I’m working 100% in Perspective.

Another option, I haven’t test it

ds = your dataset
headers = system.dataset.getColumnHeaders(ds)
if ds.columnCount >= 4:
    headers[3] = "new name"
ds.setColumnNames​(headers)

Note this won’t work for BasicStreamingDataset

3 Likes

setColumnNames is only implemented on BasicDataset, so it’s going to be more fragile than any of the other methods that work on all datasets.

4 Likes

I have the following code:

def getDailyDT_StatusData(lineNumber,day,returnSize = 2000):
	tpl = getDailyDT_TagPathsList(lineNumber)
	startTime = system.date.setTime(day,6,30,0)
	endTime = system.date.addDays(startTime,1)
	
	values = system.tag.queryTagHistory([path + '/Active' for path in tpl],startTime,endTime,includeBoundingValues = 1, returnSize = returnSize, returnFormat = 'Wide')
	names = system.dataset.getColumnHeaders(values)
	values.setColumnNames([names[0]] + [name.split('/',-1)[-2] for name in names[1:]])
	
	return values

This code is in a Project Script Library.

When I call it from the Script Console, it executes as expected.

When I call it from an Expression:

runScript('Daily_DT.getDailyDT_StatusData',0,{../TabContainer.props.currentTabIndex},{../Row_1/DateTimeInput.props.value})

I get an Error_ExpressionEval(“Error executing script for runScript() expression:Daily_DT.getDailyDT_StatusData”)

Which honestly isn’t that helpful.

If I comment out the line with setColumnNames the script will execute. I’m just curious if there is some kind of scoping issue here, because that’s the only thing I can think of that is different.