1

flight_data is dataframe in panda:

  for c in flight_data.columns:
      if ('Delay' in c):
          flight_data[c].fillna(0, inplace = True)

How do I do this in 1 line using lambda function?

map(lambda c: flight_data[c].fillna(0, inplace = True), list(filter(lambda c : 'Delay' in c, flight_data.columns)))

Why aren't these two equivalent?

When printing out the data, NaN is not replaced by 0.

5
  • 5
    Don't do this. You aren't interested in the iterable that map produces, only the side effect of calling the fillna method on each element of the original sequence. Keep the for loop as it is. Commented Oct 27, 2018 at 14:55
  • In Python 3, the function defined by your lambda expression isn't called until you actually iterate over the return value of map; until then, you just have a bunch of method calls waiting to happen. Commented Oct 27, 2018 at 14:57
  • @chepner +1, thank you, but can you clarify your second comment please, the map does have return value, which are result dataframe of fillna operations right? Do you mean I need to do "for x in map" ..? Commented Oct 27, 2018 at 15:01
  • Is there a way to do above operation (do fillna for column names that contain 'Delay') in 1 line? perhaps by using apply? I tried but failed on using apply, as each function applies to row/column data only. Commented Oct 27, 2018 at 15:03
  • 1
    map effectively just creates a wrapper around the original iterable. When you retrieve an element from it, you pull an element from the original "through" the function being mapped over it. Only then does the function actually get called. By contrast, in Python 2, map immediately called the function on each element of the iterable, return a list of the return values. Commented Oct 27, 2018 at 20:43

1 Answer 1

2

Don't use lambda

lambda only obfuscates logic here. Just specify in-scope columns and use fillna directly:

cols = df.filter(like='Delay').columns
df[cols] = df[cols].fillna(0)

How do I do this in 1 line using lambda function?

But to answer your question, you can do this without relying on side-effects of map or a list comprehension:

df = df.assign(**df.pipe(lambda x: {c: x[c].fillna(0) for c in x.filter(like='Delay')}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.