2017-03-22 18 views
1

我正在使用Python來分析一大組CSV數據。此數據包含給定時間戳和主機對的4種不同類型的度量標準,其中度量類型顯示在每行的第一個字段中。這裏有一個簡單的例子:隱式確定應該使用哪個字典

metric,timestamp,hostname,value 
metric1,1488063747,example01.net,12 
metric2,1488063747,example01.net,23 
metric3,1488063747,example01.net,34 
metric4,1488063747,example01.net,45 
metric1,1488063788,example02.net,56 
metric2,1488063788,example02.net,67 
metric3,1488063788,example02.net,78 
metric4,1488063788,example02.net,89 

因此,對於每一個row(實際上,一個列表的列表內的列表)我做的時間戳和主機名組成的指數:

idx = row[1] + ',' + row[2] 

現在的基礎上,第一個字段(列表元素)的內容,我做類似:

if row[0] == 'metric1': metric_dict[idx] = row[3] 

我爲4個指標中的每一個都做了這些。它有效,但似乎應該有更好的方法。似乎我需要以某種方式隱式或間接地根據行[0]的內容選擇要使用的字典,但是我的搜索沒有得到結果。在這種情況下,4 if行不難處理,但文件中包含更多度量標準類型並不常見。是否有可能做到這一點,並留下來閱讀列表清單後需要多少字典?謝謝。

+2

你可以窩在*另一個*快譯通這些類型的字典,說'metrics',其中鍵是'」 metric1「,這些值是合適的字典,所以,'metric [row [0]] [idx]'是你最終使用的。 –

回答

0

問題:沒有足夠的字跡。

解決方案:

conversion_dict = {'metric1': metric1_dict, 'metric2': metric2_dict} 

for row: 
    conversion_dict[row[0]][idx] = row[3] 
0

爲何不像

output = {} 
for row in rows: 
    # assuming this data is already split 

    if not row[0] in output: 
     output[row[0]] = {} 
    idx = row[1] + ',' + row[2] 
    output[row[0]][idx] = row[3] 
0

如果你做了很多表操縱的,你可能會發現pandas庫有幫助的。如果我理解正確的話,你現在要做什麼:

import pandas as pd 
from StringIO import StringIO 

s = StringIO("""metric,timestamp,hostname,value 
metric1,1488063747,example01.net,12 
metric2,1488063747,example01.net,23 
metric3,1488063747,example01.net,34 
metric4,1488063747,example01.net,45 
metric1,1488063788,example02.net,56 
metric2,1488063788,example02.net,67 
metric3,1488063788,example02.net,78 
metric4,1488063788,example02.net,89 
""") 

df = pd.read_csv(s) 
df.pivot(index="timestamp", columns='metric',values='value') 

這產生了:

metric  metric1 metric2 metric3 metric4 
timestamp          
1488063747  12  23  34  45 
1488063788  56  67  78  89