i hobby programmer , solve real-world problem having. user of software system exposes api allows 1 update/add records using get. have imported data modify system pandas , don't know best way iterate on rows , dynamically create url.
i create url each row similar to:
below have included beginning of code setup:
import pandas pd import requests r site = 'http://fakesite.org/servlet/erp?' payload = dict() payload['_form'] = 'ad1' payload['_evt'] = 'add' payload['_rtn'] = 'data' payload['fc'] = 'add' payload['_out'] = 'xml' payload['_eot'] = 'true' data = {'f1': ['a','a','a'], 'f3': ['hello', 'goodbye', 'hello_again'], 'f16': ['ea','bx','ca']} df = pd.dataframe(data)
the "payload" dictionary created parameters don't change request, these hard coded , not part of pandas data frame. combine dictionary values in each row of data frame before passing request.get
i think need use either use either apply or itterrows can't seem figure out.
how should write code?
not sure why want use pandas, can list of payloads iterate through with
f1 = ['a','a','a'] f3 = ['hello', 'goodbye', 'hello_again'] f16 = ['ea','bx','ca'] payloads = [dict(payload,**{'f1': f1, 'f3': f3, 'f16': f16}) f1,f3,f16 in zip(f1,f3,f16)]
edit: if must use pandas then, use to_dict('records')
discussed in this stackoverflow question. then, documents explain how formulate actual requests.
payloads = [dict(payload, **params) params in df.to_dict('records')] gets = [requests.get(site, params=payload) payload in payloads] # check urls thought r in gets: print r.url