How to create a table with rows and columns in python
Being able to quickly organize our data into a more readable format, such as when data wrangling, can be extremely helpful in order to analyze the data and plan the next steps. Python offers the ability to easily turn certain tabular data types into nicely formatted plain-text tables, and that’s with the tabulate function. install tabulateWe first install the tabulate library using pip install in the command line: pip install tabulate import tabulate functionWe then import the tabulate function from the tabulate library in our code: from tabulate import tabulate And now we are ready to use the tabulate function! tabular data types supported by tabulateThe tabulate function can transform any of the following into an easy to read plain-text table: (from the tabulate documentation)
list of listsFor example, if we have the following list of lists: table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]] We can turn it into into a much more readable plain-text table using the tabulate function: print(tabulate(table)) Since the first list in the list of lists contains the names of columns as its elements, we can set it as the column or header names by passing ‘firstrow’ as the argument for the headers parameter: print(tabulate(table, headers='firstrow')) The 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')) I prefer to use the ‘fancy_grid’ argument for tablefmt: print(tabulate(table, headers='firstrow', tablefmt='fancy_grid')) dictionary of iterablesWe can create the same table above using a dictionary: info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]} In the case of a dictionary, the 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: print(tabulate(info, headers='keys')) And of course we can use the tablefmt parameter to improve the table’s appearance: print(tabulate(info, headers='keys', tablefmt='fancy_grid')) adding an indexWe can also add an index to our table with the showindex parameter: We can add a custom index by passing in an iterable to the showindex parameter. For example, if we want the index to start at 1, we can pass in a range object as the argument: missing valuesIf we remove ‘Jennifer’ from the above info dictionary, our table will contain an empty field: print(tabulate({'First Name': ['John', 'Mary'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}, headers="keys", tablefmt='fancy_grid')) If there any 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: print(tabulate({'First Name': ['John', 'Mary'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}, headers="keys", tablefmt='fancy_grid', missingval='N/A')) I hope this tutorial on how to easily create nicely formatted tables using the tabulate function was helpful. Thank you for reading! How do you make a tabular form in Python?Data from lists can also be printed in tabular format.. 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.. Which command is used to create a table in Python?Creating a Table. To create a table in MySQL, use the "CREATE TABLE" statement.
How do you create a DataFrame table in Python?Method - 3: Create Dataframe from dict of ndarray/lists. import pandas as pd.. # assign data of lists.. data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}. # Create DataFrame.. df = pd.DataFrame(data). # Print the output.. print(df). How do you put data into a table in Python?Python MySQL – Insert Data Into a Table. Connect to the MySQL database server by creating a new MySQLConnection object.. Initiate a MySQLCursor object from the MySQLConnection object.. Execute the INSERT statement to insert data into the table.. Close the database connection.. |