Call two pandas columns in a function?

Home / Uncategorized / Call two pandas columns in a function?

Question:
I have a bunch of URLs I am trying to write to files. I store the URLs in a pandas dataframe.

The dataframe has two columns: url and id. I am trying to request each URL from url and write it to a file named id.

Here is what I got so far:
def get_link(url):
file_name = os.path.join(‘/mypath/foo/bar’, df.id)
try:
r = requests.get(url)
except Exception as e:print("Failded to get " + url)
else:with open(file_name, ‘w’) as f:
f.write(r.text)

df.url.apply(lambda l: get_link(l))

But when I call the function, it obvioulsly fails, since os.path.join expects a string and not a series. Hence I get the error join() argument must be str or bytes, not ‘Series’

Any ideas how I can simultaenously call df.id and df.url?

Thank you/R


Answer:
You can enhance your function to take the id_ parameter in addition to url.
def get_link(url, id_):
file_name = os.path.join(‘/mypath/foo/bar’, id_)
try:
r = requests.get(url)
except ConnectionError, MissingSchema as e:print("Failded to get " + url)
else:with open(file_name, ‘w’) as f:
f.write(r.text)

Then just iterate through your dataframe to call your function.
for idx, row in df.iterrows():get_link(url=row.url, id_=row.id)
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *