Python 如果引发System.ArgumentException,如何将dataframe行传递到API?

Python 如果引发System.ArgumentException,如何将dataframe行传递到API?,python,python-3.x,pandas,iterator,api-design,Python,Python 3.x,Pandas,Iterator,Api Design,我有这样的df: id 1 2 3 我需要遍历dataframe(frame df中只有1列),并将每个ID传递到一个API中,在API中,它在等号后面显示&leadId=。即&leadId=1 我一直在尝试以下代码: lst = [] for index,row in df.iterrows(): url = 'https://url.com/path/to?username=string&password=string&leadId=index' xml_

我有这样的df:

id
1
2
3
我需要遍历dataframe(frame df中只有1列),并将每个ID传递到一个API中,在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中,并得到相同的错误

先谢谢你

编辑:

通过以下代码行将数据帧转换为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正在尝试将字符串“行”转换为不可能的整数

更新您的url字符串

url = 'https://url.com/path/to?username=string&password=string&leadId={}'.format(row)

我只是通过执行
type(df.Id)
来检查类型。我得到'pandas.core.series.series`我确实打印了(键入(行)),我将列更改为int,并将URL中的索引更改为row,但仍然得到相同的错误对转换为int的方式进行了编辑。添加了完整的回溯我运行了您的行,并成功地将列转换为int32。但是,当我为迭代运行上述代码时,仍然得到了打印时要添加的相同错误(键入(行)),我看到df.iterrows()中索引行的int32
: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)
我正在进行前面的api调用,以获取填充当前df的ID。填充的df是我需要传递到API以获取更多信息的df。希望有意义。将列转换回字符串。不走运