我在Python中解析得不好,因为我试图将这个MATLAB代码翻译成Python,这个代码基本上是逐行分析大数据表,但收效甚微。到目前为止,我已经能够将excel数据表导入python,在python中的第二个if之后,几乎没有成功地分配数组的新值。
clear all; clc;
T=input('Ingrese el numero de filas en el BOM: ');
asig=sprintf('A1:AU%g',T); %Asign arrays to analyze
%Data transfer from excel
[num,BOM,raw]=xlsread('HRS','BOM',asig);
c=47; %columns to analyze
k=0;
%Clean of Product Definition
for i=1:1:T
mk=strcmp(BOM(i,2),'Product Definition');
while mk ~= 1
k=k + 1;
for j=1:1:c
BOM_NPD(k,j)=BOM(i,j);
end
mk=1;
end
end
在Python 中
import pandas as pd
import numpy as np
#Obtencion de tabla de archivo excel
BOM_aslist = pd.read_excel(r'')
BOM = np.array(BOM_aslist)
T=len(BOM)
c=10 #Numero de Columnas
k=-1; x=0; l=0
BOM_NPD=np.zeros((736,c))
#Clean of "Product Definition"
for i in range(1,T):
BOM_NPD = BOM_NPD.copy()
#print (BOM[i,1])
if BOM[i,1] == 'Product Definition':
l+=1
else:
if l==1:
k=0;
k+=1
for j in range(0,c):
x+=1
#print(BOM[i,j])
BOM_NPD[k,j]=BOM[i,j]
print (BOM_NPD)
我基本上得到了这个:无法将字符串转换为浮点:"工作说明">
所以错误很明显,"无法将字符串转换为浮点";
我认为错误是由于这些行,我添加了一些评论
BOM_NPD=np.zeros((736,c)) # BOM_NPD is initialized as a numpy float array
#Clean of "Product Definition"
for i in range(1,T):
BOM_NPD = BOM_NPD.copy()
#print (BOM[i,1])
if BOM[i,1] == 'Product Definition':
l+=1
else:
if l==1:
k=0;
k+=1
for j in range(0,c):
x+=1
#print(BOM[i,j])
BOM_NPD[k,j]=BOM[i,j] # Here is the problem, maybe your BOM[i, j] is a string, so you can't put a string into a float element
此外,代码也不是很清楚,请尝试提供更多细节
import pandas as pd
import numpy as np
#Obtencion de tabla de archivo excel
BOM_aslist = pd.read_excel(r'')
BOM = np.array(BOM_aslist)
T=len(BOM)
c=10 #Numero de Columnas
k=-1; x=0; l=0
BOM_NPD=pd.DataFrame(index=range(736),columns=range(c))
#Clean of "Product Definition"
for i in range(1,T):
#print (BOM[i,1])
if BOM[i,1] == 'Product Definition':
l+=1
else:
if l==1:
k=0;
k+=1
for j in range(0,c):
x+=1
#print(BOM[i,j])
BOM_NPD.iloc[k,j]=BOM[i,j]
print (BOM_NPD)