我對Python很新,我一直試圖檢測從導入的csv文件中的數據創建的列表中的缺失數據,以便我可以使用matplotlib繪製系列而不會出現錯誤。如何在Python中檢測並掩蓋導入的csv文件中的缺失數據?
我告訴你我有:
import numpy as np
# import matplotlib.pyplot as plt
import csv
from pylab import *
res = csv.reader(open('cvs_file_with_data.csv'), delimiter=',')
res.next() # do not read header
ColOneData = []
ColTwoData = []
ColThreeData = []
for col in res:
ColOneData.append(col[0])
ColTwoData.append(col[1])
ColThreeData.append(col[2])
print ColOneData # I got here the following ['1', '2', '3', '4', '5']
print ColTwoData # I got here the following ['1', '2', '', '', '5']
print ColThreeData # I got here the following ['', '', '3', '4', '']
ColTwoData_M = np.ma.masked_where(ColTwoData == '', ColTwoData) # This does not work
我需要例如屏蔽空值'',以便我可以無誤地繪製系列。任何建議來解決這個問題?
Regards ...
嗨,我不是故意刪除列表中的空白或缺失的數據。我需要掩蓋它,以便在使用matplotlib繪圖時相應的標記爲空。例如。如果我嘗試繪製ColOneData和ColTwoData,因爲它們現在會出現錯誤。 'plt.plot(ColOneData,ColTwoData)#這將產生一個錯誤 show()' – Jose
@Jose:如果丟失的數據表示爲'0',那該怎麼辦?這是否正確繪製? –