如果 System.ArgumentException 被引发,如何将数据帧行传递到 API?
How to pass row of dataframe into API if System.ArgumentException being raised?
我有 df 看起来像这样:
id
1
2
3
我需要遍历数据帧(帧 df 中只有 1 列)并将每个 ID 传递到 API,其中在等号后显示 &leadId=
。即 &leadId=1
我一直在尝试这个代码:
lst = []
for index,row in df.iterrows():
url = 'https://url.com/path/to?username=string&password=string&leadId=index'
xml_data1 = requests.get(url).text
lst.append(xml_data1)
print(xml_data1)
但我得到错误:
System.ArgumentException: Cannot convert index to System.Int32.
我的代码没有将索引值传递给 api 中的参数,我做错了什么?我也尝试将行传递到 API 并得到相同的错误。
提前谢谢你。
编辑:
通过这行代码将 dataframe 转换为 int:
df = df.astype(int)
将 API 参数中的以下内容更改为行而不是索引。
for index,row in df.iterrows():
url = 'https://url.com/path/to?username=string&password=string&leadId=row'
xml_data1 = requests.get(url).text
lst.append(xml_data1)
print(xml_data1)
得到同样的错误。
编辑2:
完整追溯:
System.ArgumentException: Cannot convert index to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
您应该将 int 转换为 API 期望的类型
df.id=df.id.astype('int32')
您当前的 url 字符串 API 正在尝试将字符串 'row' 转换为整数,这是不可能的。
更新您的 url 字符串
url = 'https://url.com/path/to?username=string&password=string&leadId={}'.format(row)
我有 df 看起来像这样:
id
1
2
3
我需要遍历数据帧(帧 df 中只有 1 列)并将每个 ID 传递到 API,其中在等号后显示 &leadId=
。即 &leadId=1
我一直在尝试这个代码:
lst = []
for index,row in df.iterrows():
url = 'https://url.com/path/to?username=string&password=string&leadId=index'
xml_data1 = requests.get(url).text
lst.append(xml_data1)
print(xml_data1)
但我得到错误:
System.ArgumentException: Cannot convert index to System.Int32.
我的代码没有将索引值传递给 api 中的参数,我做错了什么?我也尝试将行传递到 API 并得到相同的错误。
提前谢谢你。
编辑:
通过这行代码将 dataframe 转换为 int:
df = df.astype(int)
将 API 参数中的以下内容更改为行而不是索引。
for index,row in df.iterrows():
url = 'https://url.com/path/to?username=string&password=string&leadId=row'
xml_data1 = requests.get(url).text
lst.append(xml_data1)
print(xml_data1)
得到同样的错误。
编辑2:
完整追溯:
System.ArgumentException: Cannot convert index to System.Int32.
Parameter name: type ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
您应该将 int 转换为 API 期望的类型
df.id=df.id.astype('int32')
您当前的 url 字符串 API 正在尝试将字符串 'row' 转换为整数,这是不可能的。
更新您的 url 字符串
url = 'https://url.com/path/to?username=string&password=string&leadId={}'.format(row)