CSV时间序列子集选择和数据聚合

时间:2015-05-10 18:53:22

标签: python matlab csv

以下是表格的一部分:

21 , F    ,  -59981 , 2015-05-08 00:00:04 , F1
21 , P    ,       1 , 2015-05-08 00:00:04 , Shed
21 , P    ,       7 , 2015-05-08 00:00:05 , PowerS
21 , P    ,       0 , 2015-05-08 00:00:05 , PowerG
21 , P    ,     205 , 2015-05-08 00:00:05 , Panel1
21 , P    ,     129 , 2015-05-08 00:00:05 , Panel2
21 , P    ,     145 , 2015-05-08 00:00:05 , Panel3
21 , V    , -120789 , 2015-05-08 00:00:05 , VrmsA
21 , V    , -120746 , 2015-05-08 00:00:05 , VrmsB
21 , I    ,   -1750 , 2015-05-08 00:00:05 , I11
21 , I    ,    -319 , 2015-05-08 00:00:05 , I12
21 , I    ,    -988 , 2015-05-08 00:00:05 , I21
21 , I    ,    -512 , 2015-05-08 00:00:05 , I22
21 , I    ,   -1790 , 2015-05-08 00:00:05 , I31
21 , I    ,    -131 , 2015-05-08 00:00:05 , I32
21 , F    ,  -59994 , 2015-05-08 00:00:05 , F2
21 , F    ,  -59982 , 2015-05-08 00:00:05 , F1
21 , P    ,       2 , 2015-05-08 00:00:05 , Shed
21 , P    ,       6 , 2015-05-08 00:00:06 , PowerS
21 , P    ,       0 , 2015-05-08 00:00:06 , PowerG
21 , P    ,     205 , 2015-05-08 00:00:06 , Panel1
21 , P    ,     129 , 2015-05-08 00:00:06 , Panel2
21 , P    ,     146 , 2015-05-08 00:00:06 , Panel3
21 , V    , -120750 , 2015-05-08 00:00:06 , VrmsA
21 , V    , -120708 , 2015-05-08 00:00:06 , VrmsB
21 , I    ,   -1748 , 2015-05-08 00:00:06 , I11
21 , I    ,    -318 , 2015-05-08 00:00:06 , I12
21 , I    ,    -986 , 2015-05-08 00:00:06 , I21
21 , I    ,    -512 , 2015-05-08 00:00:06 , I22
21 , I    ,   -1794 , 2015-05-08 00:00:06 , I31
21 , I    ,    -132 , 2015-05-08 00:00:06 , I32
21 , F    ,  -60018 , 2015-05-08 00:00:06 , F2
21 , F    ,  -59982 , 2015-05-08 00:00:06 , F1

我想通过Matlab或Python操作CSV表,以便每隔一秒Panel1 Panel2 Panel3聚合一次

21 , P    ,     205 , 2015-05-08 00:00:05 , Panel1
21 , P    ,     129 , 2015-05-08 00:00:05 , Panel2
21 , P    ,     145 , 2015-05-08 00:00:05 , Panel3

将给出T1 = 205 + 129 + 145 并且可以形成T1,T2,T3的表。汇总数据可以添加到另一个CSV文件中。 还有一种简单的方法可以在matlab或python中单独绘制所有Panel1或Panel 2吗?

1 个答案:

答案 0 :(得分:1)

import csv


with open('thef.csv', 'rb') as f:
    data=list(csv.reader(f))

a= [int(i[2].strip()) for i in data if "Panel" in i[4]]

b=[sum(a[i:i+3]) for i in range(0,len(a),3)]

print a

print b
"""
Output:
[205, 129, 145, 205, 129, 146]
[479, 480]"""

myvars=[]
#my vars to generate variables T1,T2
for i in range(1,len(b)+1):
    myvars.append('T'+str(i))
print myvars

myrows= map(list,zip(myvars,b))

print myrows

with open('test.csv', 'wb') as fp:
    myf = csv.writer(fp, delimiter=',')
    myf.writerows(myrows