数据表 MATLAB 至 Python

Data tables MATLAB to Python

我在 Python 中没有得到很好的解析,因为我试图将这段基本上逐行分析大数据表的 MATLAB 代码翻译成 python,但几乎没有成功。到目前为止,我已经能够将 excel 数据 sheet 导入到 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)
        

我基本上明白了:无法将字符串转换为浮点数:'Work Instructions'

所以错误很明显,“无法将字符串转换为浮点数”

我认为错误是由于这些行造成的,我添加了一些评论

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)