2016-05-27 133 views
0

我合併,涉及到一個系統過程中的兩個的CSV文件。不過,我想對Header進行類似的更改。 csv文件的 實例我使用(示例性過程:「蟒」):更改合併CSV文件不保存

Date,  Process(python)\% Processor Time, Process(python)\Thread Count, Process(python)\Working Set 
xx/xx/xx xxxx         xxxx       xxxx 
xx/xx/xx xxxx         xxxx       xxxx 
xx/xx/xx xxxx         xxxx       xxxx 
xx/xx/xx xxxx         xxxx       xxxx 
xx/xx/xx xxxx         xxxx       xxxx 

我有一個腳本,其將截斷串除去該字符串的不需要的部分'Process(python)\'。使用打印語句 我可以驗證所希望的字符串被打印在屏幕上。

% Processor Time 
Thread Count 
Working Set 

但是,當我保存這個新的合併文件時,這些更改沒有被保存。如何確保我所做的更改將不需要的'Process(python)\'保存到輸出文件中?

我的代碼:

def merge_process_csv(path,processes): 
    for process_name in processes: 
     a = pd.read_csv(path+process_name+"_data_1.csv") 
     b = pd.read_csv(path+process_name+"_data_2.csv") 
     b = b.dropna(axis=1) 
     merged = a.merge(b, on='Date') 

     csvReader = csv.reader(merged) 
     for row in csvReader: 
      #Changes 'Process(python)\% Processor Time' into '% Processor Time' 
      row = truncate_string(row[0],"\\",1) 
      print row 

     merged.to_csv(path+process_name+".csv", index=False) 

回答

0

您正在改變csvReader而不是merged。如果你想要做調整標題行,可以更換:

csvReader = csv.reader(merged) 
for row in csvReader: 
    #Changes 'Process(python)\% Processor Time' into '% Processor Time' 
    row = truncate_string(row[0],"\\",1) 
    print row 

merged.columns = [col[-1] for col in merged.columns.str.split('\\')] 
+0

謝謝你的幫助,但我該怎麼定義'df'爲? – Catherine

+0

我的不好。這應該是「合併」。我編輯了我的答案。 –