英文:
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['StockNumber'] # 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<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
This is the full error and traceback:
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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论