我收到错误 ValueError: could not convert string to float。我意识到我的数据包含空值 (' ')。我如何删除它们?我试过过滤器,但没有用。
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']]
请您参考如下方法:
您可以排除缺少数据的列。这将删除前 4 列中任何条目不是 float 的所有行:
new_data = [row for row in data if all(isinstance(item, float) for item in row[:4])]
这会选择用于绘图的 x 和 y 值:
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()
