Python Forum
Filter and lambda question
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter and lambda question
#4
As nilamo said, filter "filters" entire subsets of original dataframe. It is similar to your previous groups attribute of a groupby questions - for every unique value company of Company column your lambda gets the dataframe sales[sales.Company==company] as an argument, then selects and sums Units column and either keeps entire group (if sum is bigger than 35) or "drop" it. So your result is a dataframe with all companies that sold more than 35 units...

Btw, df.apply() applies function to entire row or column, applying function to a "cell" holds only if you use apply with pandas serie. Quite often thats interchangeable - if you want to apply function on column of a dataframe, you can use either (trivial example)
sales.apply(lambda g: 2 * g['Units'], axis=1)  # lambda function used on entire row, selects "column"
or
sales.Units.apply(lambda g: 2 *g)  # lambda function used on "cell" - column was already selected
Usually second form is preferred.
Reply


Messages In This Thread
Filter and lambda question - by smw10c - Apr-27-2017, 03:57 PM
RE: Filter and lambda question - by nilamo - Apr-27-2017, 04:10 PM
RE: Filter and lambda question - by smw10c - Apr-27-2017, 04:34 PM
RE: Filter and lambda question - by zivoni - Apr-27-2017, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question for using map, lambda zydjohn 2 3,425 Dec-09-2017, 07:18 PM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020