**报错处理:** data = [float(val) for val in data] ValueError: could not convert string to float: ‘‘

报错处理:
data = [float(val) for val in data]
ValueError: could not convert string to float: ‘’
无法将string转成float,说明里面有string或者空字符
如果是删除空字符参考下面这个文章
解决方法参考
https://editor.csdn.net/md/?articleId=126119797
如果是string就需要打开数据集看看,取出float类型的区间[x:y]再转换

本文为学习笔记,仅是个人意见,仅供交流学习,请勿用于非法途径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import pandas as pd data = pd.read_csv('DATAA (1).txt', delimiter='\t') t = data.iloc[:, 0] x = data.iloc[:, 1] # 接下来的代码和之前一样 import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit #position plt.close('all') data=np.loadtxt('DATAA (1).txt',delimiter=',') t=data[:,0] x=data[:,1] t = t[130:790] x = x[130:790] plt.figure() plt.plot(t,x) plt.xlabel('time') plt.ylabel('position') max_val=max(x) max_i=list(x).index(max_val) #position up plt.figure() t_up=t[:max_i] x_up=x[:max_i] plt.plot(t_up,x_up,'r*') def fit1(t,v0,a1,x0): return x0+v0*t+0.5*a1*t**2 popt,pcov = curve_fit(fit1, t_up, x_up) plt.plot(t_up, fit1(t_up,*popt),'k', linewidth=2) #position down plt.figure() t_down=t[max_i:] x_down=x[max_i:] plt.plot(t_down,x_down,'r*') popt,pcov = curve_fit(fit1, t_down, x_down) plt.plot(t_down, fit1(t_down,*popt),'k', linewidth=2) #velocity n1=20 data=[] delta=t[1]-t[0] for i in range (n1,len(t)-n1): deri=(x[i+n1]-x[i-n1])/(2*n1*delta) data.append(deri) v=np.array(data) t= t[n1:-n1] plt.figure() plt.plot(t,v,'r*') #velocity up plt.figure() t_up=t[:max_i-n1] v_up=v[:max_i-n1] plt.plot(t_up,v_up,'r*') def fit2(t,v0,a): return v0+a*t popt,pcov = curve_fit(fit2, t_up, v_up) plt.plot(t_up, fit2(t_up,*popt),'k', linewidth=2) #velocity down plt.figure() t_down=t[max_i-n1:] v_down=v[max_i-n1:] plt.plot(t_down,v_down,'r*') popt,pcov = curve_fit(fit2, t_down, v_down) plt.plot(t_down, fit2(t_down,*popt),'k', linewidth=2) #acceleration n2=2 data2=[] for i in range (n2,len(v)-n2): deri=(v[i+n2]-v[i-n2])/(2*n2*delta) data2.append(deri) a=np.array(data2) t= t[n2:-n2] plt.figure() plt.plot(t,a,'r*') import statistics a_up_mean=statistics.mean(a[:max_i-n1-n2]) a_down_mean=statistics.mean(a[max_i-n1-n2:])。解决 ValueError: could not convert string to float: '0.008\t-1.2126E-4'问题
05-22
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit #position plt.close('all') data=np.loadtxt('DATAA (1).txt',delimiter=',') t=data[:,0] x=data[:,1] t = t[130:790] x = x[130:790] plt.figure() plt.plot(t,x) plt.xlabel('time') plt.ylabel('position') max_val=max(x) max_i=list(x).index(max_val) #position up plt.figure() t_up=t[:max_i] x_up=x[:max_i] plt.plot(t_up,x_up,'r*') def fit1(t,v0,a1,x0): return x0+v0*t+0.5*a1*t**2 popt,pcov = curve_fit(fit1, t_up, x_up) plt.plot(t_up, fit1(t_up,*popt),'k', linewidth=2) #position down plt.figure() t_down=t[max_i:] x_down=x[max_i:] plt.plot(t_down,x_down,'r*') popt,pcov = curve_fit(fit1, t_down, x_down) plt.plot(t_down, fit1(t_down,*popt),'k', linewidth=2) #velocity n1=20 data=[] delta=t[1]-t[0] for i in range (n1,len(t)-n1): deri=(x[i+n1]-x[i-n1])/(2*n1*delta) data.append(deri) v=np.array(data) t= t[n1:-n1] plt.figure() plt.plot(t,v,'r*') #velocity up plt.figure() t_up=t[:max_i-n1] v_up=v[:max_i-n1] plt.plot(t_up,v_up,'r*') def fit2(t,v0,a): return v0+a*t popt,pcov = curve_fit(fit2, t_up, v_up) plt.plot(t_up, fit2(t_up,*popt),'k', linewidth=2) #velocity down plt.figure() t_down=t[max_i-n1:] v_down=v[max_i-n1:] plt.plot(t_down,v_down,'r*') popt,pcov = curve_fit(fit2, t_down, v_down) plt.plot(t_down, fit2(t_down,*popt),'k', linewidth=2) #acceleration n2=2 data2=[] for i in range (n2,len(v)-n2): deri=(v[i+n2]-v[i-n2])/(2*n2*delta) data2.append(deri) a=np.array(data2) t= t[n2:-n2] plt.figure() plt.plot(t,a,'r*') import statistics a_up_mean=statistics.mean(a[:max_i-n1-n2]) a_down_mean=statistics.mean(a[max_i-n1-n2:])出现这个错误ValueError: could not convert string to float: '0.008\t-1.2126E-4'
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值