pandas implements sorting operation within mysql window function group
row_number() over (partition by xx,yy order by mm asc ,nn desc ) as sos
# Import Data
import pandas as pd
df = pd.read_excel(r'C:\Users\Administrator\Desktop\test.xlsx')
df.head(15)
|
Category |
Brand |
Top ranking |
Quantity |
0 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
199 |
1 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
200 |
2 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
35 |
3 |
Montmorillonite powder |
Smecta_Montmorillonite powder |
NaN |
54 |
4 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
55 |
5 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
56 |
6 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
67 |
7 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
70 |
8 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
69 |
9 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
88 |
10 |
Montmorillonite Powder |
Smecta_Montmorillonite Powder |
NaN |
32 |
11 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
68 |
12 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
5 |
13 |
Yiling |
Fang Sheng_montmorillonite powder |
top1000 |
78 |
1. Cumulative sorting within the group after grouping. The quantities are from high to low after grouping. The sorting is cumulative.
df1 = df.sort_values(["type", "brand", "topRanking", "quantity"], ascending=False)
df1.assign(rank=df1.assign(num=1).groupby(["type", "brand"], dropna=False)['num'].cumsum())
|
Category |
Brand |
top ranking |
Quantity |
rank |
0 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
199 |
1 |
6 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
67 |
2 |
7 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
70 |
3 |
8 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
69 |
4 |
3 |
Montmorillonite powder |
Smecta_Montmorillonite powder |
NaN |
54 |
1 |
10 |
Montmorillonite Powder |
Smecta_Montmorillonite Powder |
NaN |
32 |
2 |
4 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
55 |
1 |
12 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
5 |
2 |
1 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
200 |
3 |
11 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
68 |
4 |
2 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
35 |
5 |
13 |
Yiling |
Fang Sheng_montmorillonite powder |
top1000 |
78 |
1 |
9 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
88 |
1 |
5 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
56 |
2 |
2. After grouping, the sorting within the group is sorted according to subcategories, and there is no continuous accumulation.
df1 = df.sort_values(["type", "brand", "topRanking", "quantity"], ascending=False)
df1["rank2"] =df1.groupby(["type", "brand", "topRanking"], dropna=False).quantity.rank(method="min", ascending=False,).astype("int")
df1
|
Category |
Brand |
top ranking |
Quantity |
rank2 |
0 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
199 |
1 |
6 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
top1000 |
67 |
2 |
7 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
70 |
1 |
8 |
Montmorillonite Powder |
Fang Sheng_ Montmorillonite Powder |
NaN |
69 |
2 |
3 |
Montmorillonite powder |
Smecta_Montmorillonite powder |
NaN |
54 |
1 |
10 |
Montmorillonite Powder |
Smecta_Montmorillonite Powder |
NaN |
32 |
2 |
4 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
55 |
1 |
12 |
Montmorillonite Powder |
Kangenbei_Montmorillonite Powder |
top1000 |
5 |
2 |
1 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
200 |
1 |
11 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
68 |
2 |
2 |
Montmorillonite Powder |
Kang Enbei_Montmorillonite Powder |
NaN |
35 |
3 |
13 |
Yiling |
Fang Sheng_montmorillonite powder |
top1000 |
78 |
1 |
9 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
88 |
1 |
5 |
Yiling |
Smecta_montmorillonite powder |
top1000 |
56 |
2 |