In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The knowledge of this article "how to create a table in Python" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to create a table in Python" article.
1. Introduction
It is very helpful for data analysis if we can quickly organize our unordered data into a more readable format. Python provides the ability to easily convert certain table data types to well-formed plain text tables, which is the tabulate library.
two。 Preparatory work
Install the tabulate library:
It is very easy to install the tabulate library, which can be installed using pip, as follows:
Pip install tabulate
Import the tabulate function:
Then we need to import the tabulte function we need, as follows:
From tabulate import tabulate
After the preparatory work is done, let's give a chestnut.
3. For example, Chestnut 3.1 uses list to generate tables.
Then let's assume that we have the following data:
Table = [['First Name',' Last Name', 'Age'], [' John', 'Smith', 39], [' Mary', 'Jane', 25], [' Jennifer', 'Doe', 28]]
Then we can use the tabulate function to organize the above data into a more readable table form, as follows:
Print (tabulate (table))
The results are as follows:
Since the first list in the above list contains the name of each column, we can display the column name separately with the following parameters, as follows:
Print (tabulate (table, headers='firstrow'))
The results are as follows:
The tabulate function also provides a tablefmt parameter, which allows us to further improve the appearance of the table
The code is as follows:
Print (tabulate (table, headers='firstrow', tablefmt='grid'))
The results are as follows:
I prefer to use the fancy_grid parameter on tablefmt rather than grid, which is shown as follows:
Print (tabulate (table, headers='firstrow', tablefmt='fancy_grid'))
The results are as follows:
3.2 generate tables using dict
Of course, we can also use dictionaries to generate the corresponding tables in Python.
The code is as follows:
Info = {'First Name': [' John', 'Mary',' Jennifer'], 'Last Name': [' Smith', 'Jane',' Doe'], 'Age': [39, 25, 28]}
In the case of a dictionary, the key is usually the title of the column, and the value will be the element value of those columns. We usually specify that the key is the title of the table by passing "keys" as the parameter of the headers parameter:
Print (tabulate (info, headers='keys'))
The output is as follows:
Of course, we can also use the tablefmt parameter to improve the appearance of the table at this time, as follows:
Print (tabulate (info, headers='keys', tablefmt='fancy_grid'))
The output is as follows:
3.3 add index columns
Further, we can use the showindex parameter to add an index column to the table, as follows:
3.4 missing value processing
If we remove 'Jennifer',' from the dictionary, our table will contain a blank cell with the following code:
Print (tabulate ({'First Name': [' John', 'Mary'],' Last Name': ['Smith',' Jane', 'Doe'],' Age': [39, 25, 28]}, headers= "keys", tablefmt='fancy_grid'))
The output is as follows:
Sometimes, we think that the missing value is not very beautiful in a blank space, so we can set the default value to display it. The code is as follows:
Print (tabulate ({'First Name': [' John', 'Mary'],' Last Name': ['Smith',' Jane', 'Doe'],' Age': [39, 25, 28]}, headers= "keys", tablefmt='fancy_grid'))
The results are as follows:
The above is about the content of this article on "how to create tables in Python". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.