i have a dataframe and want to group 2 columns, which is working fine.
JavaScript
x
2
1
df.groupby(["Sektor, CustomerID"]).count().head(10)
2
JavaScript
1
15
15
1
_Order_ID_ Order_timezone Order_weight
2
AE 1298772 1 1 1
3
1298788 1 1 1
4
1298840 2 2 2
5
1298912 1 1 1
6
7
AT 1038570 1 1 1
8
1040424 1 1 1
9
1040425 3 3 3
10
1040426 2 2 2
11
1040427 1 1 1
12
1040428 1 1 1
13
1040429 2 2 2
14
15
Now the grouped dataframe is sorted by the CustomerID values. But i want to sort it by the count(). So that i have the Sektor then the CustomerIDs but the CustomerIds that occure the most should be at the top. So descending.
Expected Output should be:
JavaScript
1
15
15
1
_Order_ID_ Order_timezone Order_weight
2
AE 1298840 2 2 2
3
1298772 1 1 1
4
1298788 1 1 1
5
1298912 1 1 1
6
7
AT 1040425 3 3 3
8
1040426 2 2 2
9
1040429 2 2 2
10
1038570 1 1 1
11
1040424 1 1 1
12
1040427 1 1 1
13
1040428 1 1 1
14
15
How do i do that?
Advertisement
Answer
Use:
JavaScript
1
2
1
df1 = df.groupby(["Sektor", "CustomerID"]).count()
2
If need 10 rows in ouput:
JavaScript
1
15
15
1
df1 = df1.sort_values(['Sektor','_Order_ID_'], ascending=[True, False]).head(10)
2
print (df1)
3
_Order_ID_ Order_timezone Order_weight
4
Sektor CustomerID
5
AE 1298840 2 2 2
6
1298772 1 1 1
7
1298788 1 1 1
8
1298912 1 1 1
9
AT 1040425 3 3 3
10
1040426 2 2 2
11
1040429 2 2 2
12
1038570 1 1 1
13
1040424 1 1 1
14
1040427 1 1 1
15
If need 10 rows (if exist) per groups by Sektor
:
JavaScript
1
16
16
1
df1 = df1.sort_values(['Sektor','_Order_ID_'], ascending=[True, False]).groupby('Sektor').head(10)
2
print (df1)
3
_Order_ID_ Order_timezone Order_weight
4
Sektor CustomerID
5
AE 1298840 2 2 2
6
1298772 1 1 1
7
1298788 1 1 1
8
1298912 1 1 1
9
AT 1040425 3 3 3
10
1040426 2 2 2
11
1040429 2 2 2
12
1038570 1 1 1
13
1040424 1 1 1
14
1040427 1 1 1
15
1040428 1 1 1
16