Hướng dẫn generate table in python


The easiest way to create tables in Python is to use tablulate() function from the tabulate library.

Nội dung chính

  • Example 1: Create Table with Headers
  • Example 2: Create Table with Fancy Grid
  • Example 3: Create Table with Index Column
  • How do you create a table from a text file in Python?
  • How do you display data in a table in Python?
  • How do I convert a CSV file to a table in Python?
  • How do I create a MySQL table in Python?

Nội dung chính

  • Example 1: Create Table with Headers
  • Example 2: Create Table with Fancy Grid
  • Example 3: Create Table with Index Column
  • How do you create a table from a text file in Python?
  • How do you display data in a table in Python?
  • How do I convert a CSV file to a table in Python?
  • How do I create a MySQL table in Python?

To use this function, we must first install the library using pip:

pip install tabulate

We can then load the library:

from tabulate import tabulate

We can then use the following basic syntax to create tables:

print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always"))

The following examples show how to use this function in practice.

Example 1: Create Table with Headers

The following code shows how to create a basic table with headers:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names))

Team      Points
------  --------
Mavs          99
Suns          91
Spurs         94
Nets          88

Example 2: Create Table with Fancy Grid

The following code shows how to create a table with headers and a fancy grid:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names, tablefmt="fancy_grid"))

╒════════╤══════════╕
│ Team   │   Points │
╞════════╪══════════╡
│ Mavs   │       99 │
├────────┼──────────┤
│ Suns   │       91 │
├────────┼──────────┤
│ Spurs  │       94 │
├────────┼──────────┤
│ Nets   │       88 │
╘════════╧══════════╛

Note that the tablefmt argument accepts several different options including:

  • grid
  • fancy_grid
  • pipe
  • pretty
  • simple

Refer to the tabulate documentation for a complete list of potential table formats.

Example 3: Create Table with Index Column

The following code shows how to create a table with headers, a fancy grid, and an index column:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names, tablefmt="fancy_grid", showindex="always"))

╒════╤════════╤══════════╕
│    │ Team   │   Points │
╞════╪════════╪══════════╡
│  0 │ Mavs   │       99 │
├────┼────────┼──────────┤
│  1 │ Suns   │       91 │
├────┼────────┼──────────┤
│  2 │ Spurs  │       94 │
├────┼────────┼──────────┤
│  3 │ Nets   │       88 │
╘════╧════════╧══════════╛

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to discuss how to make a table in Python. Python provides vast support for libraries that can be used for creating different purposes. In this article we will talk about two such modules that can be used to create tables.

    Method 1: Using Tabulate module

    The 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

    pip install tabulate

    Below are some examples which depict how to create tables in python:

    Example 1

    Python3

    from tabulate import tabulate

    mydata = [

        ["Nikhil", "Delhi"],

        ["Ravi", "Kanpur"],

        ["Manish", "Ahmedabad"],

          ["Prince", "Bangalore"]

    ]

    head = ["Name", "City"]

    print(tabulate(mydata, headers=head, tablefmt="grid"))

    Output:

    Hướng dẫn generate table in python

    Example 2

    Python3

    from tabulate import tabulate

    mydata = [

        ['a', 'b', 'c'],

          [12, 34, 56],

          ['Geeks', 'for', 'geeks!']

    ]

    print(tabulate(mydata))

    Output:

    Method 2: Using PrettyTable module

    PrettyTable class inside the prettytable library is used to create relational tables in Python. It can be installed using the below command.

    pip install prettytable 

    Example:

    Python3

    from prettytable import PrettyTable

    myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])

    myTable.add_row(["Leanord", "X", "B", "91.2 %"])

    myTable.add_row(["Penny", "X", "C", "63.5 %"])

    myTable.add_row(["Howard", "X", "A", "90.23 %"])

    myTable.add_row(["Bernadette", "X", "D", "92.7 %"])

    myTable.add_row(["Sheldon", "X", "A", "98.2 %"])

    myTable.add_row(["Raj", "X", "B", "88.1 %"])

    myTable.add_row(["Amy", "X", "B", "95.0 %"])

    print(myTable)

    Output:


    How do you create a table from a text file in Python?

    write(tabulate(table)) , or even open('table. txt', 'w'). write(tabulate(table)) , – J.G.

    How do you display data in a table in Python?

    Print Data in Tabular Format in Python.

    Use the format() Function to Print Data in Table Format in Python..

    Use the tabulate Module to Print Data in Table Format in Python..

    Use the pandas.DataFrame() Function to Print Data in Table Format in Python..

    How do I convert a CSV file to a table in Python?

    Steps to Import a CSV file to SQL Server using Python.

    Step 1: Prepare the CSV File. ... .

    Step 2: Import the CSV File into a DataFrame. ... .

    Step 3: Connect Python to SQL Server. ... .

    Step 4: Create a Table in SQL Server using Python. ... .

    Step 5: Insert the DataFrame Data into the Table. ... .

    Step 6: Perform a Test..

    How do I create a MySQL table in Python?

    Creating a table in MySQL using python.

    Import mysql. connector package..

    Create a connection object using the mysql. connector. ... .

    Create a cursor object by invoking the cursor() method on the connection object created above..

    Then, execute the CREATE TABLE statement by passing it as a parameter to the execute() method..