2015-12-22 29 views
2

我收到错误ValueError: could not convert string to float。我意识到我的数据包含空值('')。我如何删除它们?我试过过滤器,但没有工作。使用excel中的数据绘制mathplotlib图(XLRD)

book = xlrd.open_workbook('bioreactorfinal.xlsx') 
sheet = book.sheets() [1] 
data = [[sheet.cell_value(r,c) for c in range (sheet.ncols)] for r in range(sheet.nrows)] 
x = sheet.col_values(3, start_rowx=1) 
y = sheet.col_values(0, start_rowx=1) 

plt.plot(x,y) 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('test') 

plt.show() 

#print(data[:100]) 

[['Hours', 'VCD (Cells/mL)', 'Volume (mL)', 'Cells', 'Container Size'], [0.0, 300000.0, 16.666666666666668, 5000000.0, 'SF100'], [24.0, 600000.0, 16.666666666666668, 10000000.0, 'SF100'], [48.0, 1200000.0, 16.666666666666668, 20000000.0, 'SF100'], [72.0, 2400000.0, 16.666666666666668, 40000000.0, 'SF100'], [72.0, 300000.0, 133.33333333333334, 40000000.0, 'SF1000'], [96.0, 600000.0, 133.33333333333334, 80000000.0, 'SF1000'], [120.0, 1200000.0, 133.33333333333334, 160000000.0, 'SF1000'], [144.0, 2400000.0, 133.33333333333334, 320000000.0, 'SF1000'], [144.0, 300000.0, 1066.6666666666667, 320000000.0, 'BR5'], [168.0, 600000.0, 1066.6666666666667, 640000000.0, 'BR5'], [192.0, 1200000.0, 1066.6666666666667, 1280000000.0, 'BR5'], [216.0, 2400000.0, 1066.6666666666667, 2560000000.0, 'BR5'], [216.0, 300000.0, 8533.333333333334, 2560000000.0, 'BR40'], [240.0, 600000.0, 8533.333333333334, 5120000000.0, 'BR40'], [264.0, 1200000.0, 8533.333333333334, 10240000000.0, 'BR40'], [288.0, 2400000.0, 8533.333333333334, 20480000000.0, 'BR40'], [288.0, 300000.0, 68266.66666666667, 20480000000.0, 'BR200'], [312.0, 600000.0, 68266.66666666667, 40960000000.0, 'BR200'], [336.0, 1200000.0, 68266.66666666667, 81920000000.0, 'BR200'], [360.0, 2400000.0, 68266.66666666667, 163840000000.0, 'BR200'], [360.0, 300000.0, 546133.3333333334, 163840000000.0, 'BR2k'], [384.0, 600000.0, 546133.3333333334, 327680000000.0, 'BR2k'], [408.0, 1200000.0, 546133.3333333334, 655360000000.0, 'BR2k'], [432.0, 2400000.0, 546133.3333333334, 1310720000000.0, 'BR2k'], [432.0, 300000.0, 4369066.666666667, 1310720000000.0, 'BR20k'], [456.0, 600000.0, 4369066.666666667, 2621440000000.0, 'BR20k'], [480.0, 1200000.0, 4369066.666666667, 5242880000000.0, 'BR20k'], [504.0, 2400000.0, 4369066.666666667, 10485760000000.0, 'BR20k'], [528.0, 4800000.0, 4369066.666666667, 20971520000000.0, 'BR20k'], [552.0, 9600000.0, 4369066.666666667, 41943040000000.0, 'BR20k'], ['', 300000.0, 139810133.33333334, '', 'Not Enough Space'], ['', 600000.0, 139810133.33333334, '', 'Not Enough Space'], ['', 1200000.0, 139810133.33333334, '', 'Not Enough Space']] 
+0

数据是什么样的? – Mel

+0

数据相当长。我无法通过这里发送。任何你可以帮忙的方式?有一张数据表,我只需要第一和第四列数据。在两列的末尾都有空单元格,有什么方法可以删除它们吗? –

+0

只需使用您的数据样本编辑您的问题。例如'print(data [:100])'。我不知道你的文件是什么样子的,'xlrd'如何读取它,所以很难提供帮助。 – Mel

回答

1

您可以排除缺少数据的列。这消除了其中的前4列中的任何条目不飘的所有行:

new_data = [row for row in data if all(isinstance(item, float) for item in row[:4])] 

这将选择xy值绘制:

x = [entry[3] for entry in new_data] 
y = [entry[0] for entry in new_data] 

现在剧情:

plt.plot(x,y) 
plt.xlabel('x') 
plt.ylabel('y') 
plt.title('test') 

plt.show() 
0

在定义了x,y之后,可以在给出plt函数之前过滤掉所有非浮点值:

from numbers import Number 
def isvalid(a, b): 
    return isinstance(a, Number) and isinstance(b, Number) 
xy = [xi,yi for xi,yi in zip(x,y) if isvalid(xi,yi)] 
x,y = zip(*xy) 

任何具有至少一个非成员的对将不会出现在给plt.plot的列表中。