error: cannot convert argument to integer in Python
error: cannot convert argument to integer in Python
我正在处理来自 Kaggle 的数据集,我想提取带有名称的 Pandas 列的标题。我使用以下代码:
def extract_patt(patt, linea):
matchObj = re.match(patt, linea)
result = ""
if matchObj:
return matchObj.group(1).lower()
else:
return ""
def extract_title(linea):
return extract_patt('^.+,\s(.+)\..+', linea)
titles = dataframe1["Name"].apply(extract_title)
title_mapping = {"": 0, "mr": 1, "miss": 2, "mrs": 3, "master": 4, "dr": 5, "rev": 6, "major": 7, "col": 7, "mlle": 8, "mme": 8, "don": 9, "lady": 10, "countess": 10, "jonkheer": 10, "sir": 9, "capt": 7, "ms": 2}
for k in title_mapping:
titles[titles == k] = title_mapping[k]
dataframe1["Title"] = titles
但是,当我 运行 Azure 机器学习平台上的这段代码作为 Python 代码时,出现以下错误:
Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
data:text/plain,Caught exception while executing function: Traceback (most recent call last):
File "C:\server\invokepy.py", line 176, in batch
rutils.RUtils.DataFrameToRFile(outlist[i], outfiles[i])
File "C:\server\RReader\rutils.py", line 28, in DataFrameToRFile
rwriter.write_attribute_list(attributes)
File "C:\server\RReader\rwriter.py", line 59, in write_attribute_list
self.write_object(value);
File "C:\server\RReader\rwriter.py", line 121, in write_object
write_function(flags, value.values())
File "C:\server\RReader\rwriter.py", line 104, in write_objects
self.write_object(value)
File "C:\server\RReader\rwriter.py", line 121, in write_object
write_function(flags, value.values())
File "C:\server\RReader\rwriter.py", line 71, in write_integers
self.write_integer(value)
File "C:\server\RReader\rwriter.py", line 147, in write_integer
self.writer.WriteInt32(value)
File "C:\server\RReader\BinaryIO\binarywriter.py", line 23, in WriteInt32
self.WriteData(self.Int32Format, data)
File "C:\server\RReader\BinaryIO\binarywriter.py", line 14, in WriteData
self.stream.write(pack(format, data))
error: cannot convert argument to integer
---------- End of error message from Python interpreter ----------
Start time: UTC 09/29/2015 07:47:02
End time: UTC 09/29/2015 07:47:13
问题可能出在映射代码中,因为如果我删除它,我将有一列包含标题而不是整数。
编辑: 我也尝试了以下而不是 for 循环来映射,但我有同样的错误:
dataframe1["Title"].replace(title_mapping, inplace=True)
根据我的经验,问题代码是代码 titles[titles == k] = title_mapping[k]
中的 titles == k
。表达式titles == k
的值类型是boolean类型。
在Python中,boolean类型是整型值类型的一种。 False值等于0,所有非零整数都是True值。
但是map'titles'的key的value类型应该是string类型所以报错信息是"cannot convert argument to integer".
此致。
我 运行 遇到了同样的问题,同样是泰坦尼克号数据集。我首先使用 Azure 的内置 'Project Columns' 删除了机票和机舱号列,然后将文件推送到 Python 脚本中,现在它可以工作了。
idk 那些专栏中有什么困扰它?有人发布了一条消息,其中第一行中的空值可能是一个问题,MS 表示即将修复错误。
我正在处理来自 Kaggle 的数据集,我想提取带有名称的 Pandas 列的标题。我使用以下代码:
def extract_patt(patt, linea):
matchObj = re.match(patt, linea)
result = ""
if matchObj:
return matchObj.group(1).lower()
else:
return ""
def extract_title(linea):
return extract_patt('^.+,\s(.+)\..+', linea)
titles = dataframe1["Name"].apply(extract_title)
title_mapping = {"": 0, "mr": 1, "miss": 2, "mrs": 3, "master": 4, "dr": 5, "rev": 6, "major": 7, "col": 7, "mlle": 8, "mme": 8, "don": 9, "lady": 10, "countess": 10, "jonkheer": 10, "sir": 9, "capt": 7, "ms": 2}
for k in title_mapping:
titles[titles == k] = title_mapping[k]
dataframe1["Title"] = titles
但是,当我 运行 Azure 机器学习平台上的这段代码作为 Python 代码时,出现以下错误:
Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
data:text/plain,Caught exception while executing function: Traceback (most recent call last):
File "C:\server\invokepy.py", line 176, in batch
rutils.RUtils.DataFrameToRFile(outlist[i], outfiles[i])
File "C:\server\RReader\rutils.py", line 28, in DataFrameToRFile
rwriter.write_attribute_list(attributes)
File "C:\server\RReader\rwriter.py", line 59, in write_attribute_list
self.write_object(value);
File "C:\server\RReader\rwriter.py", line 121, in write_object
write_function(flags, value.values())
File "C:\server\RReader\rwriter.py", line 104, in write_objects
self.write_object(value)
File "C:\server\RReader\rwriter.py", line 121, in write_object
write_function(flags, value.values())
File "C:\server\RReader\rwriter.py", line 71, in write_integers
self.write_integer(value)
File "C:\server\RReader\rwriter.py", line 147, in write_integer
self.writer.WriteInt32(value)
File "C:\server\RReader\BinaryIO\binarywriter.py", line 23, in WriteInt32
self.WriteData(self.Int32Format, data)
File "C:\server\RReader\BinaryIO\binarywriter.py", line 14, in WriteData
self.stream.write(pack(format, data))
error: cannot convert argument to integer
---------- End of error message from Python interpreter ----------
Start time: UTC 09/29/2015 07:47:02
End time: UTC 09/29/2015 07:47:13
问题可能出在映射代码中,因为如果我删除它,我将有一列包含标题而不是整数。
编辑: 我也尝试了以下而不是 for 循环来映射,但我有同样的错误:
dataframe1["Title"].replace(title_mapping, inplace=True)
根据我的经验,问题代码是代码 titles[titles == k] = title_mapping[k]
中的 titles == k
。表达式titles == k
的值类型是boolean类型。
在Python中,boolean类型是整型值类型的一种。 False值等于0,所有非零整数都是True值。
但是map'titles'的key的value类型应该是string类型所以报错信息是"cannot convert argument to integer".
此致。
我 运行 遇到了同样的问题,同样是泰坦尼克号数据集。我首先使用 Azure 的内置 'Project Columns' 删除了机票和机舱号列,然后将文件推送到 Python 脚本中,现在它可以工作了。
idk 那些专栏中有什么困扰它?有人发布了一条消息,其中第一行中的空值可能是一个问题,MS 表示即将修复错误。