How do you print a table pattern in python?

Introduction to Python Print Table

Python is quite a powerful language when it comes to its data science capabilities. Moreover, the Printing tables within python are sometimes challenging, as the trivial options provide you with the output in an unreadable format. We got you covered. There are multiple options to transform and print tables into many pretty and more readable formats. If you are working on python in a Unix / Linux environment, then readability can be a huge issue from the user’s perspective.

How to Print Table in Python?

There are several ways that can be utilized to print tables in python, namely:

  • Using format[] function to print dict and lists
  • Using tabulate[] function to print dict and lists
  • texttable
  • beautifultable
  • PrettyTable

Reading data in a tabular format is much easier as compared to an unstructured format, as given below:

  • Pos, Lang, Percent, Change
  • 1, Python, 33.2, UP
  • 2, Java, 23.54, DOWN
  • 3, Ruby, 17.22, UP
  • 5, Groovy, 9.22, DOWN
  • 6, C, 1.55, UP
  • 10, Lua, 10.55, DOWN

Tabular Format

Pos       Lang    Percent  Change
1             Python     33.2          UP
2            Java         23.54     DOWN
3            Ruby       17.22         UP
5           Groovy     9.22      DOWN
6              C            1.55           UP
10          Lua         10.55        DOWN

How can we Print Tables in Python?

We tend to use the information from a dictionary or a list quite frequently. One way of printing the same can be:

1. Unformatted Fashion

Let us take an example to understand this in detail

Code:

## Python program to print the data
d = {1: ["Python", 33.2, 'UP'],
2: ["Java", 23.54, 'DOWN'],
3: ["Ruby", 17.22, 'UP'],
10: ["Lua", 10.55, 'DOWN'],
5: ["Groovy", 9.22, 'DOWN'],
6: ["C", 1.55, 'UP']
} 
print ["Pos,Lang,Percent,Change"]
for k, v in d.items[]:
    lang, perc, change = v
    print [k, lang, perc, change]

Output:

The above example’s output is quite unreadable; let’s take another example of how can we print readable tables in python.

2. Formatted Fashion

Code:

## Python program to print the data
d = {1: ["Python", 33.2, 'UP'],
2: ["Java", 23.54, 'DOWN'],
3: ["Ruby", 17.22, 'UP'],
10: ["Lua", 10.55, 'DOWN'],
5: ["Groovy", 9.22, 'DOWN'],
6: ["C", 1.55, 'UP']
}
print ["{:12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
    print[format_row.format[team, *row]]

Output:

Note: Here, we are utilizing the format function to define the white spaces to print before each data point so that the resultant output looks like a table.

4. By utilizing the Tabulate Function

Let’s have another example to understand the same in quite some detail.

Code:

## Python program to understand the usage of tabulate function for printing tables in a tabular format
from tabulate import tabulate
data = [[1, 'Liquid', 24, 12],
[2, 'Virtus.pro', 19, 14],
[3, 'PSG.LGD', 15, 19],
[4,'Team Secret', 10, 20]]
print [tabulate[data, headers=["Pos", "Team", "Win", "Lose"]]]

Output:

The best part is, that you do not need to format each print statement every time you add a new item to the dictionary or the list. There are several other ways as well that can be utilized to print tables in python, namely:

  • texttable
  • beautifultable
  • PrettyTable
  • Otherwise, Pandas is another pretty solution if you are trying to print your data in the most optimized format.

5. Print table using pandas in detail

Pandas is a python library that provides data handling, manipulation, and a diverse range of capabilities in order to manage, alter and create meaningful metrics out of your dataset. Let’s take the below example in order to understand the print table option with pandas in detail.

Code:

## Python program to understand, how to print tables using pandas data frame
import pandas
data = [[1, 'Liquid', 24, 12],
[2, 'Virtus.pro', 19, 14],
[3, 'PSG.LGD', 15, 19],
[4,'Team Secret', 10, 20]]
headers=["Pos", "Team", "Win", "Lose"]
print[pandas.DataFrame[data, headers, headers]]
print['                                         ']
print['-----------------------------------------']
print['                                         ']
print[pandas.DataFrame[data, headers]]

Output:

How does it work?

  • We imported the panda’s library using the import statement
  • >> import pandas
  • Thereafter declared a list of list and assigned it to the variable named “data.”
  • in the very next step, we declared the headers
  • >> headers=[“Pos”, “Team”, “Win”, “Lose”]

How can we print this List in a Formatted Readable Structure?

  • Pandas have the power of data frames, which can handle, modify, update and enhance your data in a tabular format.
  • We have utilized the data frame module of the pandas library along with the print statement to print tables in a readable format.

Conclusion

Reading data in a tabular format is much easier as compared to an unstructured format. Utilizing the capability of python print statements with numerous functions can help you attain better readability for the same.

Recommended Articles

This is a guide to Python Print Table. Here we discuss the introduction to Python Print Table, and how to print tables with different examples. You can also go through our other related articles to learn more –

  1. Python Range Function
  2. Type Casting in Python
  3. Python if main
  4. Python List Functions

How do I print a 123 pattern in Python?

Python program to print pattern 1 12 123 Firstly, we will initialize a variable num=3. The first outer loop is used to handle a number of rows and the inner loop is used to handle a number of columns. print[j, end=” “] is used to display numbers and the other print[“”] is used for the next line after each row.

How do I print a pattern side by side in Python?

To print the letters side by side you have to concatenate the individual lines. That generally means splitting the lines, joining the corresponding lines, then putting the combined lines back together. It helps that your letters are in a rectangular block, so you don't have to work out the padding for each line.

How do you print a table from 2 to 10 in Python?

Example:.
number = int[input ["Enter the number of which the user wants to print the multiplication table: "]].
# We are using "for loop" to iterate the multiplication 10 times..
print ["The Multiplication Table of: ", number].
for count in range[1, 11]:.
print [number, 'x', count, '=', number * count].

How do you print a grid in Python?

Make it a function.
Write a function print_grid[n] that takes one integer argument and prints a grid just like before, BUT the size of the grid is given by the argument..
For example, print_grid[9] prints the grid at the top of this page..
print_grid[3] would print a smaller grid:.
print_grid[15] prints a larger grid:.

Chủ Đề