-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from autonomio/new
Major improvements to table and line types
- Loading branch information
Showing
7 changed files
with
185 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,161 @@ | ||
import pandas as pd | ||
from IPython.core.display import display, HTML | ||
|
||
from ..utils.utils import table_prep | ||
|
||
def table(data, | ||
title="Descriptive Stats", | ||
sub_title="", | ||
table_width=630, | ||
indexcol_width=150, | ||
return_html=False): | ||
|
||
def table(data, title="Descriptive Stats", table_title=""): | ||
'''Displays a publication quality data table | ||
with any number of columns and aspects. Works | ||
best for 6-7 or less columns datasets. | ||
"""PRETTY TABLE | ||
data | DataFrame | a dataframe with three columns of numeric data | ||
title | str | the main title of the table | ||
sub_title | str | shown below the title | ||
table_width | int | the width of the table in pixels | ||
indexcol_width | int | the width of th index column in pixels. | ||
Width of the data columns is computed based on `table_width` and | ||
`indexcol_width` values. | ||
USE | ||
=== | ||
NOTE: you have to input a dataframe with exactly | ||
three columns of data! | ||
''' | ||
|
||
html = ''' | ||
<style type=\"text/css\"> | ||
.tg { | ||
border-collapse:collapse; | ||
border-spacing:0; | ||
border:none;} | ||
.tg td { | ||
font-family: Arial, sans-serif; | ||
font-size:14px; | ||
padding:10px 5px; | ||
border-style:solid; | ||
border-width:0px; | ||
overflow:hidden; | ||
word-break:normal; | ||
} | ||
.tg th { | ||
font-family:Arial, sans-serif, | ||
sans-serif; | ||
font-size:14px; | ||
font-weight:normal; | ||
padding:10px 5px; | ||
border-style:solid; | ||
border-width:0px; | ||
overflow:hidden; | ||
word-break:normal; | ||
} | ||
.tg .tg-index { | ||
font-family:Verdana, Geneva, sans-serif !important; | ||
text-align: left; | ||
padding-left: 10px; | ||
vertical-align:top | ||
} | ||
.tg .tg-anay { | ||
font-family:Verdana, Geneva, sans-serif !important; | ||
text-align:right; | ||
vertical-align:top | ||
} | ||
.tg .tg-jua3 { | ||
font-weight:bold; | ||
font-family:Verdana, Geneva, sans-serif !important; | ||
text-align:right;vertical-align:top | ||
} | ||
hr { | ||
height: 1px; | ||
background-color: #333; | ||
padding: 0px; | ||
margin: 0px; | ||
} | ||
.hr2 { | ||
height: 2px !important; | ||
background-color: #333; | ||
} | ||
table { | ||
table-layout: fixed; | ||
width: 500px; | ||
border-style:hidden; | ||
border-collapse: collapse;\" | ||
margin-top: 0px; | ||
padding-top: 0px; | ||
} | ||
.title { | ||
font-family: Arial, sans-serif; | ||
font-style: italic; | ||
font-size: 22px; | ||
font-weight: bold; | ||
padding-bottom: 0px; | ||
} | ||
.sub_title { | ||
font-size: 18px; | ||
font-family: Arial, sans-serif; | ||
margin-top: 8px !important; | ||
padding-bottom: 12px; | ||
} | ||
</style> | ||
<table class=\"tg\"> | ||
<colgroup> | ||
<col style=\"width: _INDEXCOL_WIDTH_px\"> | ||
<col style=\"width: _COL_WIDTH_px\"> | ||
<col style=\"width: _COL_WIDTH_px\"> | ||
<col style=\"width: _COL_WIDTH_px\"> | ||
<col style=\"width: _COL_WIDTH_px\"> | ||
</colgroup> | ||
<p class='title'> _TABLE_TITLE_ </p> | ||
<p class='sub_title'> _TABLE_SUBTITLE_ </p> | ||
<hr align=\"left\", width=\"_TABLE_WIDTH_\"> | ||
<tr> | ||
<th class=\"tg-index\"></th> | ||
''' | ||
|
||
# add columns | ||
for col in data.columns: | ||
html += '<th class=\"tg-anay\">' + str(col) + '</th>' | ||
|
||
ast.table(df[['Age','Fare','Pclass']]) | ||
for index in data.index: | ||
|
||
html += '</tr><tr><td class=\"tg-index\">' + str(index) + '</td>' | ||
|
||
for col in data.columns: | ||
|
||
html += '<td class=\"tg-jua3\">' + f'{data.loc[index][col]:,}' + '</td>' | ||
|
||
html +=''' | ||
</tr> | ||
</table> | ||
<hr align=\"left\", width=\"_TABLE_WIDTH_\"> | ||
''' | ||
|
||
col_width = int((table_width - indexcol_width) / len(data.columns)) | ||
|
||
html = html.replace('_TABLE_TITLE_', title) | ||
html = html.replace('_TABLE_SUBTITLE_', sub_title) | ||
html = html.replace('_TABLE_WIDTH_', str(table_width)) | ||
html = html.replace('_COL_WIDTH_', str(col_width)) | ||
html = html.replace('_INDEXCOL_WIDTH_', str(indexcol_width)) | ||
|
||
display(HTML(html)) | ||
|
||
PARAMETERS | ||
---------- | ||
data :: a dataframe with three columns of numeric data | ||
title :: title to be used immediately above the table | ||
table_title :: main title, which is useful when 'title' | ||
parameter is used for description or table number etc. | ||
""" | ||
|
||
data = table_prep(data) | ||
|
||
display(HTML("<style type=\"text/css\"> .tg {border-collapse:collapse;border-spacing:0;border:none;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;} .tg .tg-ejgj{font-family:Verdana, Geneva, sans-serif !important;;vertical-align:top} .tg .tg-anay{font-family:Verdana, Geneva, sans-serif !important;;text-align:right;vertical-align:top} .tg .tg-jua3{font-weight:bold;font-family:Verdana, Geneva, sans-serif !important;;text-align:right;vertical-align:top} h5{font-family:Verdana;} h4{font-family:Verdana;} hr{height: 3px; background-color: #333;} .hr2{height: 1px; background-color: #333;} </style> <table class=\"tg\" style=\"undefined;table-layout: fixed; width: 500px; border-style: hidden; border-collapse: collapse;\"> <colgroup> <col style=\"width: 150px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> <col style=\"width: 120px\"> </colgroup> <h5>" + str(table_title) + "</h5> <h4><i>" + str(title) + "</i></h4> <hr align=\"left\", width=\"630\"> <tr> <th class=\"tg-ejgj\"></th> <th class=\"tg-anay\">median</th> <th class=\"tg-anay\">mean</th> <th class=\"tg-anay\">std</th> <th class=\"tg-anay\">total</th> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[0] + "</td> <td class=\"tg-jua3\">" + str(data['median'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][0]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][0]) + "</td> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[1] + "</td> <td class=\"tg-jua3\">" + str(data['median'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][1]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][1]) + "</td> </tr> <tr> <td class=\"tg-ejgj\">" + data.index[2] + "</td> <td class=\"tg-jua3\">" + str(data['median'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['mean'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['std'][2]) + "</td> <td class=\"tg-jua3\">" + str(data['sum'][2]) + "</td> </tr> </table> <hr align=\"left\", width=\"630\">")) | ||
if return_html: | ||
return print(html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters