只有一个长度的数组,但Python未能识别它。

huangapple go评论77阅读模式
英文:

only one length arrays but python not recognizing that it is

问题

昨天这段代码运行得很完美,但现在我得到了这个错误

只有长度为1的数组可以转换为Python标量。

import os
import re
import mouse 
import time
import numpy 
import pandas as pd
import keyboard as kb
import subprocess as sub
import pyautogui as pyAuto

from re import findall
from numpy import ndim
from pathlib import Path
from numpy import ndarray

stockNumber = xlEntries['StockNumber']  # 存储库存号的列

# 使用while循环来查找行号以及所需行号的每个规格名称和值
j = 0  # 重置while循环的计数器
while (j < fileCount):
        rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
        rowNumber.append(int(rowNumbertemp))
        specNametemp = xlEntries.loc[rowNumber[j],"Pat's Proposed Change"]
        specName.append(specNametemp)
        specValuetemp = xlEntries.loc[rowNumber[j],"StringValue"]
        specValue.append(specValuetemp)
        j = j + 1

这是完整的错误和回溯:

Traceback (most recent call last):
  File "c:\Users\jmcintosh\LIDT PRoject (copy)\LIDT Project\NEW EDMUND OPTICS LIDT\LIDT_MULTIPLE_FILE_TEST.py", line 59, in <module>
    rowNumber.append(int(rowNumbertemp))
                     ^^^^^^^^^^^^^^^^^^
TypeError: only length-1 arrays can be converted to Python scalars

还有一些我没有包括的其他代码,但它与这个问题无关。如果这个问题的格式不正确,我道歉,我是SO的新手。

我尝试移除int,但它会影响我使用pandas解析的信息,所以我现在很困惑,因为它已经运行了几天,现在却不行了。

英文:

Yesterday this code worked perfectly fine but now im getting this error

only length-1 arrays can be converted to Python scalars.

import os
import re
import mouse 
import time
import numpy 
import pandas as pd
import keyboard as kb
import subprocess as sub
import pyautogui as pyAuto

from re import findall
from numpy import ndim
from pathlib import Path
from numpy import ndarray


stockNumber = xlEntries[&#39;StockNumber&#39;]  # Column of stock numbers


# While loop to find the row numbers and the the specification name and value of each of the desired row numbers
j = 0 # Reset count for while loop
while (j&lt;fileCount):
        rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
        rowNumber.append(int(rowNumbertemp))
        specNametemp = xlEntries.loc[rowNumber[j],&quot;Pat&#39;s Proposed Change&quot;]
        specName.append(specNametemp)
        specValuetemp = xlEntries.loc[rowNumber[j],&quot;StringValue&quot;]
        specValue.append(specValuetemp)
        j = j + 1

This is the full error and traceback:

Traceback (most recent call last):
  File &quot;c:\Users\jmcintosh\LIDT PRoject (copy)\LIDT Project\NEW EDMUND OPTICS LIDT\LIDT_MULTIPLE_FILE_TEST.py&quot;, line 59, in &lt;module&gt;
    rowNumber.append(int(rowNumbertemp))
                     ^^^^^^^^^^^^^^^^^^
TypeError: only length-1 arrays can be converted to Python scalars

There is some other code i didnt include but it doesnt apply to this. I apologize if this question isnt formatted right im vey new to SO.

I tried removing the int but it messed with the info im trying to parse using pandas.im at a los since it has been working for days and now its not.

答案1

得分: 0

错误消息“只有长度为1的数组可以转换为Python标量”通常在您尝试在需要标量值的地方使用NumPy数组时出现。

变量rowNumbertemp是一个NumPy数组,您正在尝试使用int()将其转换为整数。但是,您不应直接将NumPy数组转换为整数,特别是如果它包含多个值。

快速解决方案:
在此方法中,for循环遍历rowNumbertemp数组的每个元素,并将每个元素分别附加为整数到rowNumber列表中。

rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
for row in rowNumbertemp:
    rowNumber.append(int(row))

您可以快速修复它。

英文:

The error message "only length-1 arrays can be converted to Python scalars" usually occurs when you are trying to use a NumPy array where a scalar value is expected.

The variable rowNumbertemp is a NumPy array, and you are trying to convert it to an integer using int(). However, you should not convert a NumPy array to an integer directly, especially if it contains multiple values.

Fast Solution:
In this approach, the for loop goes through each element of the rowNumbertemp array and appends each element separately as an integer to the rowNumber list.

rowNumbertemp = xlEntries[stockNumber == partNumbers[j]].index.to_numpy()
for row in rowNumbertemp:
    rowNumber.append(int(row))

You may fix it quickly.

huangapple
  • 本文由 发表于 2023年7月20日 21:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定