Home > OS >  Transpose a data table to a time-series table
Transpose a data table to a time-series table

Time:11-25

I am wondering how I can transpose my data (1 row = parameter) to time-series (1 row = 1 DateTime) I tried pivot_table from pandas but ... no column in output

I expect to have the values grouped by DateTime (Index), then 1 column for each TagName in order to have Value as Table values

#df = my sample of data
df = pd.DataFrame(data= csv, columns = ['DateTime','TagName','Value'])
df.pivot_table(index='DateTime',columns='TagName',values='Value',aggfunc=np.mean)

original data :

1

My output with pivot_table :

2

Thanks for your help.

my sample of data:

{'DateTime': {0: '2021-10-23 10:14:29.7270000',
 ​1: '2021-10-23 10:14:29.7270000',
 ​2: '2021-10-23 10:14:29.7270000',
 ​3: '2021-10-23 10:14:29.7270000',
 ​4: '2021-10-23 10:14:29.7270000',
 ​5: '2021-10-23 10:14:29.7270000',
 ​6: '2021-10-23 10:14:29.7270000',
 ​7: '2021-10-23 10:14:29.7270000',
 ​8: '2021-10-23 10:14:29.7270000',
 ​9: '2021-10-23 10:14:29.7270000'},
​'TagName': {0: 'DepollutionEntree.ChemineeOuvert',
 ​1: 'DepollutionEntree.ConsigneDepol',
 ​2: 'DepollutionEntree.TempForming',
 ​3: 'DepollutionSortie.ChemineeOuvert',
 ​4: 'DepollutionSortie.ConsigneDepol',
 ​5: 'DepollutionSortie.TempForming',
 ​6: 'Etuve.DebitGaz',
 ​7: 'FibrageB1_DebitEauDilution.PV',
 ​8: 'FibrageB2_DebitEauDilution.PV',
 ​9: 'FibrageB3_DebitEauDilution.PV'},
​'Value': {0: '0',
 ​1: '45',
 ​2: '59',
 ​3: '0',
 ​4: '66',
 ​5: '62',
 ​6: '6492604',
 ​7: '920.399963378906',
 ​8: '920.039978027344',
 ​9: '912'}}

CodePudding user response:

Try with pivot:

output = df.pivot("DateTime", "TagName", "Value")

>>> output 
TagName                     DepollutionEntree.ChemineeOuvert  ... FibrageB3_DebitEauDilution.PV
DateTime                                                      ...                              
2021-10-23 10:14:29.7270000                                0  ...                           912

[1 rows x 10 columns]
  • Related