Có thể nhanh chóng tổ chức dữ liệu của chúng tôi thành một định dạng dễ đọc hơn, chẳng hạn như khi dữ liệu gây tranh cãi, có thể cực kỳ hữu ích để phân tích dữ liệu và lập kế hoạch cho các bước tiếp theo. Python cung cấp khả năng dễ dàng biến các loại dữ liệu bảng nhất định thành các bảng văn bản đơn giản được định dạng độc đáo và đó là chức năng bảng.tabulate function. Trước tiên chúng tôi cài đặt thư viện Tabulation bằng cách cài đặt PIP trong dòng lệnh:tabulate library using pip install in the command line:Cách sử dụng chức năng Tabulation để tạo các bảng được định dạng độc đáo trong Python
Ảnh của Fotis Fotopoulos trên unplashCài đặt bảng
pip install tabulate
Nhập chức năng bảng
Sau đó, chúng tôi nhập chức năng Tabulation từ thư viện Tabulation trong mã của chúng tôi:tabulate function from the tabulate library in our code:
from tabulate import tabulate
Và bây giờ chúng tôi đã sẵn sàng để sử dụng chức năng Tabulation!tabulate function!
Các loại dữ liệu bảng được hỗ trợ bởi Tabulation
Hàm Tabulation có thể chuyển đổi bất kỳ điều nào sau đây thành một bảng văn bản đơn giản dễ đọc: [từ tài liệu Tabulation]tabulate function can transform any of the following into an easy to read plain-text table: [from the tabulate documentation]
- Danh sách danh sách hoặc một số khác của Iterables
- Danh sách hoặc một điều khác có thể đi được của Dicts [phím là cột]
- Dict of Iterables [phím làm cột]
- Mảng numpy hai chiều
- Mảng bản ghi Numpy [tên dưới dạng cột]
- pandas.DataFrame
Danh sách danh sách
Ví dụ: nếu chúng ta có danh sách danh sách sau:
table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
Chúng ta có thể biến nó thành một bảng văn bản đơn giản dễ đọc hơn bằng cách sử dụng hàm bảng:tabulate function:
print[tabulate[table]]
Vì danh sách đầu tiên trong danh sách danh sách chứa tên của các cột là các phần tử của nó, chúng ta có thể đặt nó thành tên cột hoặc tên tiêu đề bằng cách truyền ‘firstrow, làm đối số cho tham số tiêu đề:headers parameter:
print[tabulate[table, headers='firstrow']]
Hàm Tabulation cũng chứa tham số bảng, cho phép chúng tôi cải thiện sự xuất hiện của bảng của chúng tôi bằng cách sử dụng giả đồ giả:tabulate function also contains a tablefmt parameter, which allows us to improve the appearance of our table using pseudo-graphics:
print[tabulate[table, headers='firstrow', tablefmt='grid']]
Tôi thích sử dụng đối số ‘Fancy_Grid cho bảng điều khiển:tablefmt:
print[tabulate[table, headers='firstrow', tablefmt='fancy_grid']]
Từ điển của Iterables
Chúng ta có thể tạo cùng một bảng ở trên bằng cách sử dụng từ điển:
info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}
Trong trường hợp của một từ điển, các phím sẽ là các tiêu đề cột và các giá trị sẽ là các phần tử của các cột đó. Chúng tôi chỉ định rằng các phím sẽ là các tiêu đề bằng cách truyền ‘phím, làm đối số cho tham số tiêu đề:keys will be the column headers, and the values will be the elements of those columns. We specify that the keys will be the headers by passing ‘keys’ as the argument for the headers parameter:
Và tất nhiên chúng ta có thể sử dụng tham số bảng để cải thiện sự xuất hiện của bảng:tablefmt parameter to improve the table’s appearance:
print[tabulate[info, headers='keys', tablefmt='fancy_grid']]
thêm một chỉ mục
Chúng tôi cũng có thể thêm một chỉ mục vào bảng của mình với tham số ShowIndex:index to our table with the showindex parameter:
Chúng ta có thể thêm một chỉ mục tùy chỉnh bằng cách chuyển trong một tham số ShowIndex. Ví dụ: nếu chúng ta muốn chỉ mục bắt đầu ở mức 1, chúng ta có thể chuyển trong một đối tượng phạm vi như đối số:showindex parameter. For example, if we want the index to start at 1, we can pass in a range object as the argument:
giá trị bị mất
Nếu chúng tôi xóa ‘Jennifer, khỏi từ điển thông tin trên, bảng của chúng tôi sẽ chứa một trường trống:info dictionary, our table will contain an empty field:
from tabulate import tabulate
0Nếu có bất kỳ giá trị còn thiếu nào trong bảng của chúng tôi, chúng tôi có thể chọn những gì cần điền chúng bằng cách sử dụng tham số còn thiếu. Giá trị mặc định cho thiếu là một chuỗi trống. Nếu chúng ta thay đổi nó thành N/A, thì đây là những gì mà bảng của chúng ta sẽ trông như thế nào:missing values in our table, we can choose what to fill them in with using the missingval parameter. The default value for missingval is an empty string. If we change it to ‘N/A’, this is what what our table will look like:
from tabulate import tabulate
1Tôi hy vọng hướng dẫn này về cách dễ dàng tạo các bảng được định dạng độc đáo bằng cách sử dụng chức năng Tabulation là hữu ích. Cảm ơn bạn đã đọc!
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnUsing Tabulate module
Trong bài viết này, chúng tôi sẽ thảo luận về cách làm một bảng trong Python. Python cung cấp hỗ trợ rộng lớn cho các thư viện có thể được sử dụng để tạo ra các mục đích khác nhau. Trong bài viết này, chúng tôi sẽ nói về hai mô -đun như vậy có thể được sử dụng để tạo bảng.tabulate[] method is a method present in the tabulate module which creates a text-based table output inside the python program using any given inputs. It can be installed using the below command
from tabulate import tabulate
2Phương pháp 1: Sử dụng mô -đun Tabulation
Phương thức Tabulation [] là một phương thức có trong mô-đun bảng tạo ra đầu ra bảng dựa trên văn bản bên trong chương trình Python bằng cách sử dụng bất kỳ đầu vào nào. Nó có thể được cài đặt bằng lệnh dưới đây
Python3
Dưới đây là một số ví dụ mô tả cách tạo bảng trong Python:
ví dụ 1
table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
3table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
5table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
6table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[table]]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
6table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[table]]
5table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[table]]
7table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
6print[tabulate[table]]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[table, headers='firstrow']]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[table, headers='firstrow']]
3print[tabulate[table, headers='firstrow']]
4print[tabulate[table, headers='firstrow']]
4from tabulate import tabulate
4 from tabulate import tabulate
5from tabulate import tabulate
6 from tabulate import tabulate
7from tabulate import tabulate
8from tabulate import tabulate
9 table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0Output:
print[tabulate[table, headers='firstrow']]
6from tabulate import tabulate
9 table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[table, headers='firstrow']]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[table, headers='firstrow', tablefmt='grid']]
1print[tabulate[table, headers='firstrow']]
4
Python3
Dưới đây là một số ví dụ mô tả cách tạo bảng trong Python:
ví dụ 1
table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[table, headers='firstrow', tablefmt='fancy_grid']]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}
1table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}
3table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
6from tabulate import tabulate
4 from tabulate import tabulate
5from tabulate import tabulate
6 from tabulate import tabulate
7print[tabulate[table]]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[info, headers='keys']]
5table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[info, headers='keys']]
7table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[info, headers='keys']]
9print[tabulate[table, headers='firstrow']]
4print[tabulate[table, headers='firstrow']]
4print[tabulate[table, headers='firstrow', tablefmt='grid']]
3print[tabulate[info, headers='keys', tablefmt='fancy_grid']]
3Output:
from tabulate import tabulate
8from tabulate import tabulate
9 table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0Using
PrettyTable module
print[tabulate[table, headers='firstrow']]
6from tabulate import tabulate
9 table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
0print[tabulate[table, headers='firstrow']]
9table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4print[tabulate[table, headers='firstrow', tablefmt='grid']]
1print[tabulate[table, headers='firstrow']]
4from tabulate import tabulate
3Example:
Python3
Các
Ví dụ 2
from tabulate import tabulate
09from tabulate import tabulate
10table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
14table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
16from tabulate import tabulate
08from tabulate import tabulate
09from tabulate import tabulate
19table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
23table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
25from tabulate import tabulate
08from tabulate import tabulate
09from tabulate import tabulate
28table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
32table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
34from tabulate import tabulate
08from tabulate import tabulate
09from tabulate import tabulate
37table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
41table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
43from tabulate import tabulate
08from tabulate import tabulate
09from tabulate import tabulate
46table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
32table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
52from tabulate import tabulate
08from tabulate import tabulate
09from tabulate import tabulate
55table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
12table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
14table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
4from tabulate import tabulate
61from tabulate import tabulate
08Các
print[tabulate[table, headers='firstrow', tablefmt='grid']]
3from tabulate import tabulate
73Output: