简体   繁体   中英

Is there a way to create a lambda equivalent of this function call in Pandas?

Using this function and call:

  def top(df, n=3, column='tip_pct'):
      return df.sort_values(by=column, ascending=False)[:n]

  tips.groupby('smoker').apply(top)

I was able to obtain this output:

在此处输入图像描述

Is there a way to show the same output using a lambda function?

Since the function is a one-liner, you can just copy the return expression into the body of the lambda.

tips.groupby('smoker').apply(lambda df, n=3, column='tip_pct': df.sort_values(by=column, ascending=False)[:n])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM