如何使用iterrow访问csv文件的每个值并保存在python中的变量中

时间:2019-03-22 15:12:39

标签: python pandas csv

我有一个csv文件,我正在使用pandas在python中读取此文件。我想读取文件的每一行,并使用循环将该行的每个值存储在变量中。我应该怎么做。

Station MAC, First time seen, Last time seen, Power, # packets, BSSID, Probed ESSIDs
macaddrees, 2018-06-22 11:23:45, 2018-06-22 11:23:45, -78,        8, 52:62:90:00:03:01,
macaddress, 2018-06-22 11:23:46, 2018-06-22 11:24:05, -73,        4, 14:30:04:BB:19:A2,
macaddress, 2018-06-22 11:23:52, 2018-06-22 11:24:12, -73,        5, (not associated) ,
macaddress, 2018-06-22 11:23:43, 2018-06-22 11:24:12, -71,        9, not assocaited,
macadress, 2018-06-22 11:23:52, 2018-06-22 11:23:52, -70,        2, (not associated) ,
macaddress, 2018-06-22 11:23:48, 2018-06-22 11:24:01, -69,       11, NAN,
macaddress, 2018-06-22 11:23:46, 2018-06-22 11:24:12, -65,       15, NAN,
macaddress, 2018-06-22 11:24:12, 2018-06-22 11:24:12, -62,        2, (not associated) ,
macaddress, 2018-06-22 11:24:01, 2018-06-22 11:24:12, -54,        2, NAN,
macaddress, 2018-06-22 11:23:49, 2018-06-22 11:24:12, -48,       97, NAN,
macaddress, 2018-06-22 11:23:43, 2018-06-22 11:24:12, -35,       54, NAN,UET Smart University
macaddress, 2018-06-22 11:23:47, 2018-06-22 11:24:03, -23,      108, NAN,
macaddress, 2018-06-22 11:23:49, 2018-06-22 11:23:49,  -1,        9, NAN,

我尝试过的是

for index, row in df.iterrows():
Station_Mac=(row["Station MAC"])
First_Time_seen=(row["First time seen"])
print(Station_Mac)

我想获取变量Station MAC,BSSID等以将其传递给有效负载函数以将数据发送到数据库  

import requests

url = "http://localhost/api/customers_data/create.php"

payload = "{\n \"Station_MAC\": \"56:12:34:345\",\n \n \"First_time_seen\": \"12:45:66:32\",\n \n \"Last_time_seen\": \"-89\",\n \n \"Power\": \"-48\",\n \n \"Packets\": \"12\",\n \n \"BSSID\": \"65:08:08:98\",\n \n \"Probed_ESSIDs\": \"93\"\n}"
headers = {
    'cache-control': "no-cache",
    'Postman-Token': "ca64cf56-19e8-4e6c-85a7-1741355ced79"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

1 个答案:

答案 0 :(得分:0)

请务必验证版本

np.__version__ '1.14.3'

pd.__version__ '0.24.1'

all_vars = np.array(df).flatten().tolist()