Pandas/Numpy已更改的文件类型现在读入Get Calculation E

2024-09-30 12:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Pandas读取Excel文件,从2组9列中提取Numpy数组,并创建一个复数数组来进行复数矩阵乘法。原始数据只是十进制值,但根据值来自的列来决定它们是实值还是虚值。我最初是以xlsx的形式编写从和Excel文件读取的代码的。然后我把它改成一个csv文件源,我得到相同的3X3矩阵,在这两种情况下,我合并成一个复杂的矩阵。当我运行代码时,我得到以下错误。你知道吗

        TypeError  Traceback (most recent call last)
<ipython-input-107-e87255037c7f> in <module>()
     11     while counter < count_net_r:
     12         n = counter # int
---> 13         net = (net_r[n] + 1.0j * net_x[n])
     14         counter = counter + 1
     15         net_seq.append(net)

TypeError: can't multiply sequence by non-int of type 'complex'

我在文件中读取的代码:

df = pd.read_csv('Report.csv')

# uses Pandas .loc to extract and create the network equivalent R per phase components into an array
# for multiplication to calculate the sequence matrix - REAL Part
r = df.loc[df['Code\n'] == 'Network Equivalent', 'NetEq Z R AA':'NetEq Z R CC']
rr = r.shape[0]
net_r = r.values.reshape(rr,3,3)

# uses Pandas .loc to extract and create the network equivalent X per phase components into an array
# for multiplication to calculate the sequence matrix - COMPLEX Part
x = df.loc[df['Code\n'] == 'Network Equivalent', 'NetEq Z X AA':'NetEq Z X CC']
xx = x.shape[0]
net_x = x.values.reshape(xx,3,3)

# loop to concatenate the R & X into one array of complex numbers
# if the R and X matrices are of unequal lengths then prints unequal length so it can be solved and attempted again
count_net_r = len(net_r) # int
count_net_x = len(net_x) # int
net = [] # list
net_seq = [] # list
counter = 0
if count_net_r != count_net_x:
    print('Network Equivalent matrices are not equivalent')
else:
    while counter < count_net_r:
        n = counter # int
        net = (net_r[n] + 1.0j * net_x[n])
        counter = counter + 1
        net_seq.append(net)
net_seq = np.array(net_seq)

我只改变了文件的读入方式。那么我需要做什么改变才能让代码正常工作呢?或者,有更好的方法吗?你知道吗


Tags: and文件theto代码pandasdfnet
1条回答
网友
1楼 · 发布于 2024-09-30 12:16:30

所以我不知道为什么,但我所做的只是在创建两个数组时更改了类型。你知道吗

# uses Pandas .loc to extract and create the network equivalent R per phase components into an array
# for multiplication to calculate the sequence matrix - REAL Part
r = df.loc[df['Code\n'] == 'Network Equivalent', 'NetEq Z R AA':'NetEq Z R CC'].astype('float')
rr = r.shape[0]
net_r = r.values.reshape(rr,3,3)

# uses Pandas .loc to extract and create the network equivalent X per phase components into an array
# for multiplication to calculate the sequence matrix - COMPLEX Part
x = df.loc[df['Code\n'] == 'Network Equivalent', 'NetEq Z X AA':'NetEq Z X CC'].astype('float')
xx = x.shape[0]
net_x = x.values.reshape(xx,3,3)

我以为即使在csv文件中输入一个数字也是一个数字。我想我错了。你知道吗

相关问题 更多 >

    热门问题