![](/img/trans.png)
[英]Python code to convert DataFrame table (Panel data?) to time-series?
[英]Transpose a data table to a time-series table
我想知道如何將我的數據(1 行 = 參數)轉換為時間序列(1 行 = 1 個日期時間)
我希望將值按日期時間(索引)分組,然后每個 TagName 有 1 列,以便將值作為表值
#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)
原始數據:
我的 output 與 pivot_table:
謝謝你的幫助。
我的數據樣本:
{'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'}}
嘗試使用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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.