Mailing List Archive

How to apply a self defined function in Pandas
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On 2021-10-31 17:25, Shaozhong SHI wrote:
> 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
>
You're passing a function to '.apply'. That has one argument,' x'.

But what is the function doing with that argument?

Nothing.

The function is just returning the result of connect(df['URL']).

df['URL'] is a column, so you're passing a column to '.urlopen', which,
of course, it doesn't understand.

So 'connect' returns False.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
> df['URL'] = df.apply(lambda x: connect(df['URL']), axis=1)

I think you need axis=0. Or use the Series, df['URL'] =
df.URL.apply(connect)
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On Sunday, 31 October 2021, Albert-Jan Roskam <sjeik_appie@hotmail.com>
wrote:

>
>
> > df['URL'] = df.apply(lambda x: connect(df['URL']), axis=1)
>
>
> I think you need axis=0. Or use the Series, df['URL'] =
> df.URL.apply(connect)
>
Any details?
I will try and let you know. Regards, David
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On Sunday, 31 October 2021, MRAB <python@mrabarnett.plus.com> wrote:

> On 2021-10-31 17:25, Shaozhong SHI wrote:
>
>> 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
>>
>> You're passing a function to '.apply'. That has one argument,' x'.
>
> But what is the function doing with that argument?
>
> Nothing.
>
> The function is just returning the result of connect(df['URL']).
>
> df['URL'] is a column, so you're passing a column to '.urlopen', which, of
> course, it doesn't understand.
>
> So 'connect' returns False.
>
>
Please expand on how.

David

> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On 2021-10-31 18:48, Shaozhong SHI wrote:
>
> On Sunday, 31 October 2021, MRAB <python@mrabarnett.plus.com> wrote:
>
> On 2021-10-31 17:25, Shaozhong SHI wrote:
>
> 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
>
> You're passing a function to '.apply'. That has one argument,' x'.
>
> But what is the function doing with that argument?
>
> Nothing.
>
> The function is just returning the result of connect(df['URL']).
>
> df['URL'] is a column, so you're passing a column to '.urlopen',
> which, of course, it doesn't understand.
>
> So 'connect' returns False.
>
>
> Please expand on how.
>
It's as simple as passing 'connect' to '.apply' as  the first argument.
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On Sun, 31 Oct 2021 at 19:28, MRAB <python@mrabarnett.plus.com> wrote:

> On 2021-10-31 18:48, Shaozhong SHI wrote:
> >
> > On Sunday, 31 October 2021, MRAB <python@mrabarnett.plus.com> wrote:
> >
> > On 2021-10-31 17:25, Shaozhong SHI wrote:
> >
> > 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
> >
> > You're passing a function to '.apply'. That has one argument,' x'.
> >
> > But what is the function doing with that argument?
> >
> > Nothing.
> >
> > The function is just returning the result of connect(df['URL']).
> >
> > df['URL'] is a column, so you're passing a column to '.urlopen',
> > which, of course, it doesn't understand.
> >
> > So 'connect' returns False.
> >
> >
> > Please expand on how.
> >
> It's as simple as passing 'connect' to '.apply' as the first argument.
>


Well, can you expand the the simplicity?

Regards, David

> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
Am Sun, Oct 31, 2021 at 07:52:18PM +0000 schrieb Shaozhong SHI:

> Well, can you expand the the simplicity?

Not sure how expanding is going to help but here's one way to
do it:

Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list('simplicity')
['s', 'i', 'm', 'p', 'l', 'i', 'c', 'i', 't', 'y']
>>>

Best,
Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
--
https://mail.python.org/mailman/listinfo/python-list
Re: How to apply a self defined function in Pandas [ In reply to ]
On Sun, 31 Oct 2021 at 18:42, Shaozhong SHI <shishaozhong@gmail.com> wrote:

>
>
> On Sunday, 31 October 2021, Albert-Jan Roskam <sjeik_appie@hotmail.com>
> wrote:
>
>>
>>
>> > df['URL'] = df.apply(lambda x: connect(df['URL']), axis=1)
>>
>>
>> I think you need axis=0. Or use the Series, df['URL'] =
>> df.URL.apply(connect)
>>
>

> Just experimented with your suggestion, but have not seen any difference.
>

Regards, David
--
https://mail.python.org/mailman/listinfo/python-list
RE: How to apply a self defined function in Pandas [ In reply to ]
When people post multiple comments and partial answers and the reply every time is to ask for more; there may be a disconnect.

Some assume that the person is just stuck but generally can go forward with a little hint. But when you keep being asked for more, maybe it means they want you to do it for them, as in some asking about HW.

I am not joining this one. ????


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net@python.org> On Behalf Of Karsten Hilbert
Sent: Sunday, October 31, 2021 4:00 PM
To: python-list@python.org
Subject: Re: How to apply a self defined function in Pandas

Am Sun, Oct 31, 2021 at 07:52:18PM +0000 schrieb Shaozhong SHI:

> Well, can you expand the the simplicity?

Not sure how expanding is going to help but here's one way to do it:

Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> list('simplicity')
['s', 'i', 'm', 'p', 'l', 'i', 'c', 'i', 't', 'y']
>>>

Best,
Karsten
--
GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list