Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the .agg () and .apply () methods in the groupby () method

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you how to use the .agg () and .apply () methods in the groupby () method. The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn how to use the .agg () and .apply () methods in the groupby () method.

After .groupby (), you can follow the .agg () and .apply () parameters. The functions of these two parameters are magical, but the usage is a little different.

1. Construct a DataFrame first

Import pandas as pddf = pd.DataFrame ({"year": ["2020", "2019", "2020", "2020", "2019", "2019"], "commodity": ["apple", "watermelon", "litchi", "longan", "pineapple", "pineapple"], "sales": [100200300500600]}) df

two。 Observe the results made by .agg ()

Df.groupby (["year"]) .agg (lambda x: print (x))

3. The result made with .apply ()

Df.groupby (["year"]) .apply (lambda x: print (x))

It is not difficult to see that. Apply () handles objects such as DataFrame data tables, while .agg () passes in only one column at a time.

4. Take a look at other uses of .agg ().

Df.groupby (["year"]) .agg (["sum", "mean", "max", "min"])

The above is all the content of the article "how the .agg () and .apply () methods are used in the groupby () method. Thank you for reading!" I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 238

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report