Reputation: 367
Taking a pandas dataframe of the form:
fname lname age
mark trumb 34
samb jones 26
I'd like to construct a url query using the requests module. Normally, I would construct the url like so:
payload = {'key1':'value1','key2':'value2'}
headers = {'content-type': 'application'}
base_url = 'https://website.com/'
req = requests.post(base_url,headers=headers,params=payload)
How would I best use this so that the dataframe columns become the keys, and the rows become the values?
The new output column (req) would look like this:
fname lname age req
mark trumb 34 https://website.com?fname=mark&lname=trumb&age=34
samb jones 26 https://website.com?fname=samb&lname=jones&age=26
Upvotes: 2
Views: 1140
Reputation: 394041
You can build it like this:
In [46]:
df['req'] = 'https://website.com?' + 'fname=' + df['fname'] + '&lname=' + df['lname'] + '&age=' + df['age'].astype(str)
df
Out[46]:
fname lname age req
0 mark trumb 34 https://website.com?fname=mark&lname=trumb&age=34
1 samb jones 26 https://website.com?fname=samb&lname=jones&age=26
A dynamic method can be done by joining the column names with the values:
In [106]:
def func(x):
tuple_list = list(zip('&' + x.index + '=', x.astype(str)))
return ''.join([''.join(item) for item in tuple_list])
df['req'] = r'https://website.com?' + df.apply(lambda x: func(x), axis=1)
df['req']
Out[106]:
0 https://website.com?&name=mark&lname=trumb&age=34
1 https://website.com?&name=samb&lname=jones&age=26
Name: req, dtype: object
Upvotes: 1