2015-05-10 40 views
1

這裏是一個表的一部分:CSV時間序列子集選擇和數據agregation

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?

+0

輸出應該是另一個csv文件? – Ajay

+0

是輸出應該在另一個csv文件 –

+0

T2 = 205 + 129 + 146,T3呢? – Ajay

回答

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