Home > Software design >  How to iterate through DataFrame and use the values in requests?
How to iterate through DataFrame and use the values in requests?

Time:09-27

amazing pythoners,

I am hoping to get some help with my below scenario

I have a list of a few centers based on which I want to extract employee data for each center. Earlier I was using the below method and it was working beautifully.

row[0] in the CSV file had the whole URL which looked something like this:

https://api.test.com/v1/centers/96d901bd-2fcc-4f59-91d7-de18f0b0aa90/employees?page=1&size=100
FilePath = open("Employees.csv")
CSV_File = csv.reader(FilePath)
#CSV_File.next()
header = next(CSV_File)
for row in CSV_File:
    url2 = row[0]
    CenterCode = row[1]
    

    try:
        payload={}
        payload2={}
        headers = {'Authorization': 'apikey'}
        response = requests.request("GET", url2, headers=headers, data=payload)
        EmployeesData = response.json()

        for i in EmployeesData['employees']:
            print(i['employee_id'], end = ','), print(CenterCode)




import requests
import pandas as pd
import json

## AA is my DataFrame
AA = AA[['id', 'code']]
#print(AA)
CID = AA['id']
CID2 = CID.to_string(index = False)
#print(CID2)

for index in range(len(AA)):
    #print (AA.loc[index, 'id'], AA.loc[index, 'code'])
    try:
        url2 = f"https://api.test.com/v1/centers/{CID2}/employees?page=1&size=100"
        print(url2)

    payload={}
    files=[]
    headers = {'Authorization': 'apikey'}

    response = requests.request("GET", url2, headers=headers, data=payload, files=files)
    data = response.json()
    print('Employee Guid','|','Employee Code', '|', CID2)



except Exception as e:
            print(e)

Now I have included the URL in the below new code and replaced only "Center ID" by using the F string. I am extracting Center ID from Pandas DataFrame. However, when I run the code, I am getting the error "Expecting value: line 1 column 1 (char 0)" and I guessed that it must be due to the URL, hence I tried to print the URL and found below result.

Output:-
https://api.zenoti.com/v1/centers/ee2395cb-e714-41df-98d2-66a69d38c556
96d901bd-2fcc-4f59-91d7-de18f0b0aa90/employees?page=1&size=100
Expecting value: line 1 column 1 (char 0)
https://api.zenoti.com/v1/centers/ee2395cb-e714-41df-98d2-66a69d38c556
96d901bd-2fcc-4f59-91d7-de18f0b0aa90/employees?page=1&size=100
Expecting value: line 1 column 1 (char 0)
[Finished in 4.6s]

What is happening in the above output is that I have 2 rows to test my code each containing a unique Center ID, however, both of them are getting added together and replaced by the F string in the URL hence the error

  • Related