2

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

yes - however, I should have said that I want to apply the new series the requests.post method, in order to practice passing functions to series items.. for instance, I could have 100 columns that I would not want to write out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.