How to apply a self defined function in Pandas

Shaozhong SHI shishaozhong at gmail.com
Sun Oct 31 13:25:12 EDT 2021


I defined a function and apply it to a column in Pandas.  But it does not
return correct values.

I am trying to test which url in a column full of url to see which one can
be connected to or not

def connect(url):
    try:
        urllib.request.urlopen(url)
        return True
    except:
        return False

df['URL'] = df.apply(lambda x: connect(df['URL']), axis=1)

I ran without any error, but did not return any true.

I just could not find any error with it.

Can anyone try and find out why


Regards,

David


More information about the Python-list mailing list