How to achieve intra-group ranking in Python
Today, I would like to talk to you about how to achieve intra-group ranking in Python, many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.
1. Find the maximum / minimum / average within the group
Import pandas as pddf = pd.read_excel (r "D:\ Jupyter\ data\ group sort .xlsx") df
# assign sale = df.groupby (["category"]) ["sales"]
# find the maximum sales of different categories sale_max = sale.max () sale_max
# find the minimum sales of different categories sale_min = sale.min () sale_min
# find out the average sales of different categories sale_mean = sale.mean () sale_mean
two。 Add a new field to achieve cumulative summation
Import pandas as pddf1 = pd.read_excel (r "D:\ Jupyter\ data\ group sort .xlsx") df1
# add a new field to sum the sales df1 ["cumulative sales"] = df1 ["sales"] .cumsum () df1
3. Add a new field and realize the cumulative summation within the group
Import pandas as pddf2 = pd.read_excel (r "D:\ Jupyter\ data\ group sort .xlsx") df2
Df2 ["summation by category"] = df2.groupby (["category"]) ["sales volume"] .cumsum () df2
4. Add a new column to rank sales without changing its order (no sorting)
Import pandas as pddf3 = pd.read_excel (r "D:\ Jupyter\ data\ group sort .xlsx", sheet_name = 1) df3
Df3 [in ascending order by sales "] = df3 [" sales "] .rank () df3
Other ranking rules
Df3 ["sales _ mean"] = df ["sales"] .rank (method = "average") df3
Df3 ["sales _ max"] = df ["sales"] .rank (method = "max") df3
Df3 ["sales _ min"] = df3 ["sales"] .rank (method = "min") df3
Df3 ["sales _ first"] = df3 ["sales"] .rank (method = "first") df3
5. Achieve intra-group ranking of sales according to category
Import pandas as pddf4 = pd.read_excel (r "D:\ Jupyter\ data\ group sort .xlsx", sheet_name = 1) df4
# ranking sales by category df4 ["category _ ranking"] = df4.groupby (["category"]) ["sales"] .rank () df4
Note:
1. Method: {'average',' min', 'max',' first', 'dense'}, default' average' is mainly used to set parameters with the same value when sorting
2. Na_option: {'keep',' top', 'bottom'}, default' keep' when there is a null value in the sorted data, the default value is set to keep
After reading the above, do you have any further understanding of how to achieve intra-group ranking in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.