Syntax:
tabulate(DataFrame_object, headers=’keys’, tablefmt)
Parameters:
- DataFrame_object refers to the existing DataFrame.
- Headers keep the columns in the DataFrame after converting.
- The tablefmt takes the table style.
We will see the different styles of tables by just modifying this parameter.
First, we create a DataFrame with 2 columns and we use this DataFrame in all table styles.
Note: Make sure to run this code in your environment because we use this code in all our examples. Otherwise, you will get errors.
from tabulate import tabulate
# Consider the DataFrame having 5 records
dataset=pandas.DataFrame({
'Idea':['All','water supply','electricity','drilling','electricity'],
'demography':['ap','gujarat','patna','indore','norway']},index=[2,3,4,5,6])
print("Actual: \n")
print(dataset)
Output:
Idea demography
2 All ap
3 water supply gujarat
4 electricity patna
5 drilling indore
6 electricity norway
Table 1: PSQL Format
Convert the DataFrame to psql format.
Output:
| | Idea | demography |
|----+--------------+--------------|
| 2 | All | ap |
| 3 | water supply | gujarat |
| 4 | electricity | patna |
| 5 | drilling | indore |
| 6 | electricity | norway |
+----+--------------+--------------+
Table 2: Fancy_Grid Format
Fancy_grid style is like organizing the DataFrame in a grid with neat margins.
Output:
│ │ Idea │ demography │
╞════╪══════════════╪══════════════╡
│ 2 │ All │ ap │
├────┼──────────────┼──────────────┤
│ 3 │ water supply │ gujarat │
├────┼──────────────┼──────────────┤
│ 4 │ electricity │ patna │
├────┼──────────────┼──────────────┤
│ 5 │ drilling │ indore │
├────┼──────────────┼──────────────┤
│ 6 │ electricity │ norway │
╘════╧══════════════╧══════════════╛
Table 3: Plain
Plain is similar to the plain DataFrame format. No margins are created in this format.
Output:
2 All ap
3 water supply gujarat
4 electricity patna
5 drilling indore
6 electricity norway
Table 4: HTML
The HTML code is returned as output when we specify the tablefmt as “html”. If you want to check whether the HTML code creates a table or not, run the generated HTML code in the browser.
Output:
<thead>
<tr><th style="text-align: right;"> </th><th>Idea </th><th>demography </th></tr>
</thead>
<tbody>
<tr><td style="text-align: right;"> 2</td><td>All </td><td>ap </td></tr>
<tr><td style="text-align: right;"> 3</td><td>water supply</td><td>gujarat </td></tr>
<tr><td style="text-align: right;"> 4</td><td>electricity </td><td>patna </td></tr>
<tr><td style="text-align: right;"> 5</td><td>drilling </td><td>indore </td></tr>
<tr><td style="text-align: right;"> 6</td><td>electricity </td><td>norway </td></tr>
</tbody>
</table>
When you run this HTML code in the browser, you will see the following table:
Table 5: Github
Convert to “github” format.
Output:
|----|--------------|--------------|
| 2 | All | ap |
| 3 | water supply | gujarat |
| 4 | electricity | patna |
| 5 | drilling | indore |
| 6 | electricity | norway |
Table 6: Pretty Format
The “pretty” format is same as the psql only.
Output:
| | Idea | demography |
+---+--------------+------------+
| 2 | All | ap |
| 3 | water supply | gujarat |
| 4 | electricity | patna |
| 5 | drilling | indore |
| 6 | electricity | norway |
+---+--------------+------------+
Table 7: TCSV Format
If you want to see your DataFrame in the tab which is separated by CSV, you can use “tcsv”.
Output:
2 All ap
3 water supply gujarat
4 electricity patna
5 drilling indore
6 electricity norway
Table 8: CSV Format
Convert the DataFrame to csv format.
Output:
-- ------------ ------------
2 All ap
3 water supply gujarat
4 electricity patna
5 drilling indore
6 electricity norwayApplied necessary capitalization since this is a subheading.
Table 9: Excel format
Convert the DataFrame to excel format. This is similar to CSV.
Output:
-- ------------ ------------
2 All ap
3 water supply gujarat
4 electricity patna
5 drilling indore
6 electricity norway
Conclusion
When rendering a DataFrame into a table code, we first need to import the tabulate module. We just change the tablefmt parameter to set the table style. By seeing the 8 styles, we came to know that Pandas have such a wonderful option to display the DataFrame in different ways that meet our project requirements. Make sure that you set the headers parameter to “keys”.
from https://ift.tt/f5xUil6
0 Comments