Jon Z
Jon Z

Reputation: 13

Renaming column names in lists generated by a function

I am using PIconnect to pull data from a server. It iterates through a list of points and returns a list with a column name of the summary method (AVERAGE in this case). When I run the code below, I end up with dataframe that has 300 columns all named Average. I would love to know if I can change those columns names to the point name in my script.

import pandas as pd
import PIconnect as PI
from PIconnect.PIConsts import SummaryType, TimestampCalculation

with PI.PIServer()as server:
   points = server.search('WTG*_AI_IN_TempA*')
   data = [point.summaries('5/14/2024','6/1/2024', '10m', SummaryType.AVERAGE, time_type=TimestampCalculation.EARLIEST_TIME)
   for point in points]
   data2 = pd.concat(data, axis = 1) 

When this runs here are the variables data - List - Size 309 - Values are all Dataframes data2 - DataFrame - Size (2448,309), All Columns are labelled AVERAGE.

I tried breaking the loop in different spots to used Pandas to rename the column, but nothing has worked.

Upvotes: 0

Views: 58

Answers (1)

Jason Molina
Jason Molina

Reputation: 1

One way is to use rename of pandas DataFrame.

import pandas as pd
import PIconnect as PI
from PIconnect.PIConsts import SummaryType, TimestampCalculation

with PI.PIServer() as server:
   points = server.search('WTG*_AI_IN_TempA*')
   data = [point.summaries('5/14/2024','6/1/2024', '10m', SummaryType.AVERAGE, time_type=TimestampCalculation.EARLIEST_TIME).rename(columns = {'AVERAGE': point.tag})
   for point in points]
   data2 = pd.concat(data, axis = 1)

Upvotes: 0

Related Questions