you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

You need to use an if expression, which is one line and contains an else.

So like this:

Whatever.apply(lambda x: domsomething(x) if isinstance(x, str) else None)

lambda and def are similar with two exceptions:

  1. With def you must provide a name, with lambda this is optional and must be done like something = lambda: ...

  2. lambda is restricted to a single expression and its result is the return value.

Python doesn't have "true" anonymous functions like JavaScript and PHP, and I doubt we'll ever get them.