python - Call two pandas columns in a function? -
i have bunch of urls trying write files. store urls in pandas dataframe.
the dataframe has 2 columns: url
, id
. trying request each url url
, write file named id
.
here got far:
def get_link(url): file_name = os.path.join('/mypath/foo/bar', df.id) try: r = requests.get(url) except exception e: print("failded " + url) else: open(file_name, 'w') f: f.write(r.text) df.url.apply(lambda l: get_link(l))
but when call function, obvioulsly fails, since os.path.join
expects string
, not series
. hence error join() argument must str or bytes, not 'series'
any ideas how can simultaenously call df.id
, df.url
?
thank you/r
you can enhance function take id_
parameter in addition url
.
def get_link(url, id_): file_name = os.path.join('/mypath/foo/bar', id_) try: r = requests.get(url) except connectionerror, missingschema e: print("failded " + url) else: open(file_name, 'w') f: f.write(r.text)
then iterate through dataframe call function.
for idx, row in df.iterrows(): get_link(url=row.url, id_=row.id)
Comments
Post a Comment