[Tutor] weird lambda expression -- can someone help me understand how this works

eryksun eryksun at gmail.com
Sat Dec 14 11:23:28 CET 2013


On Fri, Dec 13, 2013 at 11:21 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>
>> >>> l = lambda x: x**2
>> >>> apply(l, (3,))
>> 9
>
> The built-in function apply is deprecated in Python 2 and removed in
> Python 3.

Possibly using apply() was meant to be similar to pandas
DataFrame.apply, but the latter applies a function repeatedly to the
rows or columns along an axis. It's closer to map().

http://pandas.pydata.org/pandas-docs/dev/api.html#id6

    df = pandas.DataFrame(
           [[2, 3],
            [4, 6]],
           columns=('c0','c1'),
           index=('r0','r1'))

    >>> df
        c0  c1
    r0   2   3
    r1   4   6

    >>> r = df.apply(print)
    r0    2
    r1    4
    Name: c0, dtype: int64
    r0    3
    r1    6
    Name: c1, dtype: int64

    >>> r = df.apply(print, axis=1)
    c0    2
    c1    3
    Name: r0, dtype: int64
    c0    4
    c1    6
    Name: r1, dtype: int64


More information about the Tutor mailing list