Dưới đây là một ví dụ về đọc dữ liệu từ tệp csv bằng gấu trúc, nhóm dữ liệu theo giá trị trong một cột và sắp xếp chúng theo giá trị trong cột khác nhưng trong các nhóm đó
Original Data:
+--------------+---------+------------------+
| Project Name | Student | Submission Date |
+--------------+---------+------------------+
| Gamma | Cathy | 2019-05-16 4:30 |
| Beta | Cathy | 2019-04-17 8:6 |
| Alpha | Derek | 2019-03-26 15:3 |
| Alpha | Bruce | 2019-03-21 2:30 |
| Gamma | Ezze | 2019-05-18 20:5 |
| Alpha | Ezze | 2019-04-10 5:13 |
| Beta | Ezze | 2019-04-17 5:48 |
| Alpha | Cathy | 2019-03-29 12:37 |
| Alpha | Adam | 2019-03-26 18:5 |
| Gamma | Adam | 2019-05-17 6:57 |
| Gamma | Derek | 2019-05-14 10:12 |
| Gamma | Bruce | 2019-05-16 4:49 |
| Beta | Bruce | 2019-04-16 8:59 |
| Beta | Adam | 2019-04-17 16:10 |
| Beta | Derek | 2019-04-18 21:9 |
+--------------+---------+------------------+
Mục tiêu là nhóm các hàng theo Tên dự án và sau đó sắp xếp chúng theo Ngày gửi tương ứng của chúng
Để nhóm khung dữ liệu Pandas, chúng tôi sử dụng groupby[]. Để sắp xếp khung dữ liệu được nhóm theo thứ tự giảm dần, hãy sử dụng sort_values[]. Phương thức size[] được sử dụng để lấy kích thước khung dữ liệu
Để sắp xếp theo thứ tự giảm dần, hãy sử dụng thông tin sau trong sort_values[] −
ascending=False
Lúc đầu, hãy tạo một khung dữ liệu gấu trúc -
dataFrame = pd.DataFrame[ { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } ]
Tiếp theo, nhóm theo cột Reg_Price và sắp xếp theo thứ tự giảm dần −
dataFrame.groupby['Reg_Price'].size[].sort_values[ascending=False]
Ví dụ
Sau đây là mã
import pandas as pd # dataframe with one of the columns as Reg_Price dataFrame = pd.DataFrame[ { "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] } ] print"DataFrame...\n",dataFrame # group according to Reg_Price column and sort in descending order print"\nSorted in Descending order..."; print[dataFrame.groupby['Reg_Price'].size[].sort_values[ascending=False]]
đầu ra
Điều này sẽ tạo ra đầu ra sau -
DataFrame... Car Reg_Price 0 BMW 1000 1 Lexus 1400 2 Audi 1000 3 Mercedes 900 4 Jaguar 1700 5 Bentley 900 Sorted in Descending order... Reg_Price 1000 2 900 2 1700 1 1400 1 dtype: int64
df.sort_values[['var1','var2'],ascending=False].groupby['var1'].head[]
Ví dụ sau đây cho thấy cách sử dụng cú pháp này trong thực tế
Ví dụ. Sử dụng GroupBy và Sắp xếp trong các nhóm trong Pandas
Giả sử chúng ta có Khung dữ liệu gấu trúc sau đây hiển thị doanh số bán hàng được thực hiện tại hai địa điểm cửa hàng khác nhau
import pandas as pd
#create DataFrame
df = pd.DataFrame[{'store': ['B', 'B', 'A', 'A', 'B', 'B', 'A', 'A'],
'sales': [12, 25, 8, 14, 10, 20, 30, 30]}]
#view DataFrame
print[df]
store sales
0 B 12
1 B 25
2 A 8
3 A 14
4 B 10
5 B 20
6 A 30
7 A 30
Chúng ta có thể sử dụng cú pháp sau để nhóm các hàng theo cột cửa hàng và sắp xếp theo thứ tự giảm dần dựa trên cột doanh số
________số 8Lưu ý rằng chúng ta cũng có thể loại bỏ đối số ascending=False để sắp xếp các giá trị bán hàng theo thứ tự tăng dần Pandas Groupby được sử dụng trong các trường hợp chúng ta muốn chia nhỏ dữ liệu và đặt thành các nhóm để có thể thực hiện nhiều thao tác khác nhau trên các nhóm đó như – Tổng hợp dữ liệu, Chuyển đổi thông qua một số tính toán nhóm hoặc Lọc theo các điều kiện cụ thể được áp dụng trên các nhóm Theo những cách tương tự, chúng ta có thể thực hiện sắp xếp trong các nhóm này ví dụ 1. Hãy lấy một ví dụ về một khung dữ liệudf
ascending=False
0 ascending=False
1ascending=False
2ascending=False
3ascending=False
4ascending=False
5ascending=False
4ascending=False
5ascending=False
8ascending=False
5ascending=False
8ascending=False
02
ascending=False03
ascending=False04
ascending=False3
ascending=False06
ascending=False5
ascending=False08
ascending=False5
ascending=False10
ascending=False5
ascending=False12
ascending=False13
ascending=False14
ascending=False15
ascending=False16
ascending=False2
ascending=False18
ascending=False19
ascending=False20
đầu ra
Hãy chuyển tham số sắp xếp thành Sai
ascending=False21
ascending=False22
ascending=False16
ascending=False2
ascending=False25
ascending=False0
ascending=False27
ascending=False18
ascending=False19
ascending=False20
đầu ra
Ở đây, chúng ta thấy một khung dữ liệu với các giá trị được sắp xếp trong các nhóm
Ví dụ 2.
Bây giờ, hãy lấy một ví dụ về khung dữ liệu với độ tuổi của những người khác nhau. Sử dụng chức năng sắp xếp cùng với chức năng nhóm sẽ sắp xếp khung dữ liệu được chuyển đổi trên cơ sở các lần chuyển khóa, để tăng tốc tiềm năng.
ascending=False31
ascending=False0
ascending=False33
ascending=False34
ascending=False35
ascending=False36
ascending=False5
ascending=False38
ascending=False5
ascending=False40
ascending=False5
ascending=False42
ascending=False43
ascending=False44
ascending=False45
ascending=False5
ascending=False36
ascending=False5
ascending=False49
ascending=False5
ascending=False40
ascending=False52
ascending=False53
ascending=False54
ascending=False35
ascending=False56
ascending=False5
ascending=False58
ascending=False5
ascending=False40
ascending=False5
ascending=False56
ascending=False43
ascending=False44
ascending=False45
ascending=False5
ascending=False40
ascending=False5
ascending=False49
ascending=False5
ascending=False51
ascending=False52
ascending=False14
df
ascending=False0
ascending=False56
df
đầu ra
Hãy nhóm khung dữ liệu trên theo tên
ascending=False58
ascending=False59
ascending=False34
ascending=False81
ascending=False19
ascending=False20
đầu ra
Truyền tham số sắp xếp là Sai
ascending=False21
ascending=False22
ascending=False59
ascending=False34
ascending=False88
ascending=False0
ascending=False27
ascending=False18
ascending=False19
ascending=False20
đầu ra
Ví dụ 3.
Hãy lấy một ví dụ khác về khung dữ liệu bao gồm tốc độ tối đa của nhiều loại ô tô và xe đạp.
Chúng tôi sẽ cố gắng sắp xếp tốc độ tối đa trong các nhóm loại phương tiện.
ascending=False004
ascending=False005
ascending=False14
ascending=False14
df
ascending=False0
ascending=False010
ascending=False011
ascending=False5
ascending=False013
ascending=False5
ascending=False015
ascending=False016
ascending=False03
ascending=False018
ascending=False011
ascending=False5
ascending=False021
ascending=False5
ascending=False023
ascending=False016
ascending=False03
ascending=False018
ascending=False027
ascending=False5
ascending=False029
ascending=False5
ascending=False031
ascending=False032
ascending=False03
ascending=False018
ascending=False027
ascending=False5
ascending=False037
ascending=False5
ascending=False039
ascending=False016
ascending=False03
ascending=False018
ascending=False011
ascending=False5
ascending=False045
ascending=False5
ascending=False047
ascending=False032
ascending=False03
ascending=False018
ascending=False027
ascending=False5
ascending=False053
ascending=False5
ascending=False055
ascending=False016
ascending=False03
ascending=False018
ascending=False011
ascending=False5
ascending=False061
ascending=False5
ascending=False063
ascending=False064
ascending=False065
ascending=False066
ascending=False0
ascending=False018
ascending=False069
ascending=False5
ascending=False34
ascending=False5
ascending=False073
ascending=False074