簡體   English   中英

如何遍歷 DataFrame 並使用請求中的值?

[英]How to iterate through DataFrame and use the values in requests?

驚人的蟒蛇,

我希望對我的以下情況有所幫助

我有幾個中心的列表,我想根據這些中心為每個中心提取員工數據。 早些時候我使用下面的方法,它工作得很好。

CSV 文件中的 row[0] 有整個 URL 看起來像這樣:

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)

現在我在下面的新代碼中包含了 URL 並使用 F 字符串僅替換了“中心 ID”。 我正在從 Pandas DataFrame 中提取中心 ID。 但是,當我運行代碼時,我收到錯誤“期望值:第 1 行第 1 列(字符 0)” ,我猜這一定是由於 URL,因此我嘗試打印 URL 並找到以下結果。

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]

上面 output 中發生的事情是我有 2 行來測試我的代碼,每行都包含一個唯一的中心 ID,但是,它們都被加在一起並被 URL 中的 F 字符串替換,因此出現錯誤

有什么建議,這里可以做些什么不同的事情? 提前致謝。

如果我理解正確,中心 ID 是這樣的: CID = AA['id']
嘗試以這種方式遍歷id列:

for CID2 in AA['id']:
    try:
        url2 = f"https://api.test.com/v1/centers/{CID2}/employees?page=1&size=100"
        print(url2)
    except Exception as e:
        print(e)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM