Hướng dẫn how do you create a data table in python? - làm thế nào để bạn tạo một bảng dữ liệu trong python?

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 unplash

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.

Cài đặt bảng

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:

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:

________số 8

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
0

Nế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
1

Tô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

  • Đọc
  • Bàn luận
  • 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
    2

    Phươ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]]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    3
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    5
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    6

    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[table]]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    6

    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[table]]
    5
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[table]]
    7
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    6

    print[tabulate[table]]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[table, headers='firstrow']]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[table, headers='firstrow']]
    3
    print[tabulate[table, headers='firstrow']]
    4

    print[tabulate[table, headers='firstrow']]
    4

    from tabulate import tabulate
    4
    from tabulate import tabulate
    5
    from tabulate import tabulate
    6
    from tabulate import tabulate
    7

    from tabulate import tabulate
    8
    from tabulate import tabulate
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0

    Output:

    print[tabulate[table, headers='firstrow']]
    6
    from tabulate import tabulate
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[table, headers='firstrow']]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[table, headers='firstrow', tablefmt='grid']]
    1
    print[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]]
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[table, headers='firstrow', tablefmt='fancy_grid']]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}
    1
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}
    3
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    6

    from tabulate import tabulate
    4
    from tabulate import tabulate
    5
    from tabulate import tabulate
    6
    from tabulate import tabulate
    7

    print[tabulate[table]]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[info, headers='keys']]
    5
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[info, headers='keys']]
    7
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[info, headers='keys']]
    9
    print[tabulate[table, headers='firstrow']]
    4

    print[tabulate[table, headers='firstrow']]
    4

    print[tabulate[table, headers='firstrow', tablefmt='grid']]
    3
    print[tabulate[info, headers='keys', tablefmt='fancy_grid']]
    3

    Output:

    from tabulate import tabulate
    8
    from tabulate import tabulate
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    Using PrettyTable module

    print[tabulate[table, headers='firstrow']]
    6
    from tabulate import tabulate
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    0
    print[tabulate[table, headers='firstrow']]
    9
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    print[tabulate[table, headers='firstrow', tablefmt='grid']]
    1
    print[tabulate[table, headers='firstrow']]
    4

    from tabulate import tabulate
    3

    Example:

    Python3

    Các

    Ví dụ 2

    from tabulate import tabulate
    09
    from tabulate import tabulate
    10
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    14
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    16
    from tabulate import tabulate
    08

    from tabulate import tabulate
    09
    from tabulate import tabulate
    19
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    23
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    25
    from tabulate import tabulate
    08

    from tabulate import tabulate
    09
    from tabulate import tabulate
    28
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    32
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    34
    from tabulate import tabulate
    08

    from tabulate import tabulate
    09
    from tabulate import tabulate
    37
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    41
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    43
    from tabulate import tabulate
    08

    from tabulate import tabulate
    09
    from tabulate import tabulate
    46
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    32
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    52
    from tabulate import tabulate
    08

    from tabulate import tabulate
    09
    from tabulate import tabulate
    55
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    12
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    14
    table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]
    4
    from tabulate import tabulate
    61
    from tabulate import tabulate
    08

    Các

    print[tabulate[table, headers='firstrow', tablefmt='grid']]
    3
    from tabulate import tabulate
    73

    Output:


    Bảng dữ liệu trong Python là gì?

    DataTable là một thư viện Python để thao tác dữ liệu bảng. Nó hỗ trợ các bộ dữ liệu ngoài bộ nhớ, xử lý dữ liệu nhiều luồng và có API linh hoạt. Nếu điều này nhắc nhở bạn về dữ liệu của R. Bảng, bạn được phát hiện bởi vì gói dữ liệu của Python có liên quan chặt chẽ và lấy cảm hứng từ thư viện R.a python library for manipulating tabular data. It supports out-of-memory datasets, multi-threaded data processing and has a flexible API. If this reminds you of R's data. table, you are spot on because Python's datatable package is closely related to and inspired by the R library.

    Bạn có thể làm bàn trong Python không?

    Tạo một bảng bằng Python: Tạo một bảng trong Python rất dễ sử dụng thư viện PrettyPrint. Chỉ cần nhập mô-đun và sử dụng phương thức add_row [] của nó để thêm nhiều hàng hoặc tạo một hàng bảng khôn ngoan.Creating a table in Python is very easy using the PrettyPrint library. Simply import the module and use its add_row[] method to add multiple rows or create a table row-wise.

    Làm thế nào để bạn tạo ra một bảng theo cách thủ công trong Python?

    Làm thế nào để dễ dàng tạo bảng trong Python..
    Cài đặt bảng.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: PIP Cài đặt Tabulation ..
    Nhập chức năng bảng.....
    Danh sách danh sách.....
    Từ điển của Iterables.....
    giá trị bị mất..

    Lệnh nào được sử dụng để tạo một bảng trong Python?

    Để tạo một bảng bằng Python, bạn cần thực thi câu lệnh CREATE TABLE bằng phương thức EXECUTE [] của con trỏ của pyscopg2.CREATE TABLE statement using the execute[] method of the Cursor of pyscopg2.

    Bài Viết Liên Quan

    Chủ Đề