In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to create tables in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
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 install the tabulate library is very easy, you can install it using pip, the code is as follows:
Pip install tabulate
To 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, 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:
Thank you for reading! This is the end of the article on "how to create tables in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.