Search code examples
pythonpython-3.xpandasiteratorapi-design

How to pass row of dataframe into API if System.ArgumentException being raised?


I Have df that looks like this:

id
1
2
3

I need to iterate through the dataframe (only 1 column in the frame df)and pass each ID into an API, where it says &leadId= after the equal sign. I.e &leadId=1

I have been trying this code:

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)

but I get error:

System.ArgumentException: Cannot convert index to System.Int32.

What am I doing wrong in my code to not pass the index value into the parameter in the api? I have also tried passing row into the API and get the same error.

Thank you in advance.

edit:

converted dataframe to int by this line of code:

df = df.astype(int)

changed following to row instead of index in API parameters.

 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)

getting same error.

edit2:

full traceback:

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()

Solution

  • You should convert your int to the type which the API expects

    df.id=df.id.astype('int32')
    

    With your current url string API is trying to convert 'row' which is a string to an integer which is not possible.

    update your url string

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