Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

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

Shulou Source: shulou.com Published: 2022-06-01 05:53:28 10月03日 Update

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!

Tags: Method year content article parameters results pineapple learning help magic two functions goods objects data datasheets easy to understand more organization knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Redmi MariaDB Shulou Tech Info