{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# pandoc_tabulate" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import sphinx" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
88602male27.00013.00SSecondmanTrueNaNSouthamptonnoTrue
88711female19.00030.00SFirstwomanFalseBSouthamptonyesTrue
88803femaleNaN1223.45SThirdwomanFalseNaNSouthamptonnoFalse
88911male26.00030.00CFirstmanTrueCCherbourgyesTrue
89003male32.0007.75QThirdmanTrueNaNQueenstownnoTrue
\n", "
" ], "text/plain": [ " survived pclass sex age sibsp parch fare embarked class \\\n", "886 0 2 male 27.0 0 0 13.00 S Second \n", "887 1 1 female 19.0 0 0 30.00 S First \n", "888 0 3 female NaN 1 2 23.45 S Third \n", "889 1 1 male 26.0 0 0 30.00 C First \n", "890 0 3 male 32.0 0 0 7.75 Q Third \n", "\n", " who adult_male deck embark_town alive alone \n", "886 man True NaN Southampton no True \n", "887 woman False B Southampton yes True \n", "888 woman False NaN Southampton no False \n", "889 man True C Cherbourg yes True \n", "890 man True NaN Queenstown no True " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "titanic = sns.load_dataset(\"titanic\")\n", "titanic.tail()\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method to_html in module pandas.core.frame:\n", "\n", "to_html(buf=None, columns=None, col_space=None, colSpace=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, bold_rows=True, classes=None, escape=True, max_rows=None, max_cols=None, show_dimensions=False, notebook=False, decimal='.') method of pandas.core.frame.DataFrame instance\n", " Render a DataFrame as an HTML table.\n", " \n", " `to_html`-specific options:\n", " \n", " bold_rows : boolean, default True\n", " Make the row labels bold in the output\n", " classes : str or list or tuple, default None\n", " CSS class(es) to apply to the resulting html table\n", " escape : boolean, default True\n", " Convert the characters <, >, and & to HTML-safe sequences.=\n", " max_rows : int, optional\n", " Maximum number of rows to show before truncating. If None, show\n", " all.\n", " max_cols : int, optional\n", " Maximum number of columns to show before truncating. If None, show\n", " all.\n", " decimal : string, default '.'\n", " Character recognized as decimal separator, e.g. ',' in Europe\n", " \n", " .. versionadded:: 0.18.0\n", " \n", " Parameters\n", " ----------\n", " buf : StringIO-like, optional\n", " buffer to write to\n", " columns : sequence, optional\n", " the subset of columns to write; default None writes all columns\n", " col_space : int, optional\n", " the minimum width of each column\n", " header : bool, optional\n", " whether to print column labels, default True\n", " index : bool, optional\n", " whether to print index (row) labels, default True\n", " na_rep : string, optional\n", " string representation of NAN to use, default 'NaN'\n", " formatters : list or dict of one-parameter functions, optional\n", " formatter functions to apply to columns' elements by position or name,\n", " default None. The result of each function must be a unicode string.\n", " List must be of length equal to the number of columns.\n", " float_format : one-parameter function, optional\n", " formatter function to apply to columns' elements if they are floats,\n", " default None. The result of this function must be a unicode string.\n", " sparsify : bool, optional\n", " Set to False for a DataFrame with a hierarchical index to print every\n", " multiindex key at each row, default True\n", " index_names : bool, optional\n", " Prints the names of the indexes, default True\n", " justify : {'left', 'right'}, default None\n", " Left or right-justify the column labels. If None uses the option from\n", " the print configuration (controlled by set_option), 'right' out\n", " of the box.\n", " \n", " Returns\n", " -------\n", " formatted : string (or unicode, depending on data and options)\n", "\n" ] } ], "source": [ "help(titanic_tail.to_html)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
88602male27.00013.00SSecondmanTrueNaNSouthamptonnoTrue
88711female19.00030.00SFirstwomanFalseBSouthamptonyesTrue
88803femaleNaN1223.45SThirdwomanFalseNaNSouthamptonnoFalse
88911male26.00030.00CFirstmanTrueCCherbourgyesTrue
89003male32.0007.75QThirdmanTrueNaNQueenstownnoTrue
\n" ] } ], "source": [ "print(titanic_tail.to_html())" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pypandoc" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function convert in module pypandoc:\n", "\n", "convert(source, to, format=None, extra_args=(), encoding='utf-8', outputfile=None, filters=None)\n", " Converts given `source` from `format` `to` another.\n", " \n", " :param str source: Unicode string or bytes or a file path (see encoding)\n", " \n", " :param str to: format into which the input should be converted; can be one of\n", " `pypandoc.get_pandoc_formats()[1]`\n", " \n", " :param str format: the format of the inputs; will be inferred if input is a file with an\n", " known filename extension; can be one of `pypandoc.get_pandoc_formats()[1]`\n", " (Default value = None)\n", " \n", " :param list extra_args: extra arguments (list of strings) to be passed to pandoc\n", " (Default value = ())\n", " \n", " :param str encoding: the encoding of the file or the input bytes (Default value = 'utf-8')\n", " \n", " :param str outputfile: output will be written to outfilename or the converted content\n", " returned if None (Default value = None)\n", " \n", " :param list filters: pandoc filters e.g. filters=['pandoc-citeproc']\n", " \n", " :returns: converted string (unicode) or an empty string if an outputfile was given\n", " :rtype: unicode\n", " \n", " :raises RuntimeError: if any of the inputs are not valid of if pandoc fails with an error\n", " :raises OSError: if pandoc is not found; make sure it has been installed and is available at\n", " path.\n", "\n" ] } ], "source": [ "help(pypandoc.convert)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(['commonmark',\n", " 'docbook',\n", " 'docx',\n", " 'epub',\n", " 'haddock',\n", " 'html',\n", " 'json',\n", " 'latex',\n", " 'markdown',\n", " 'markdown_github',\n", " 'markdown_mmd',\n", " 'markdown_phpextra',\n", " 'markdown_strict',\n", " 'mediawiki',\n", " 'native',\n", " 'odt',\n", " 'opml',\n", " 'org',\n", " 'rst',\n", " 't2t',\n", " 'textile',\n", " 'twiki'],\n", " ['asciidoc',\n", " 'beamer',\n", " 'commonmark',\n", " 'context',\n", " 'docbook',\n", " 'docx',\n", " 'dokuwiki',\n", " 'dzslides',\n", " 'epub',\n", " 'epub3',\n", " 'fb2',\n", " 'haddock',\n", " 'html',\n", " 'html5',\n", " 'icml',\n", " 'json',\n", " 'latex',\n", " 'man',\n", " 'markdown',\n", " 'markdown_github',\n", " 'markdown_mmd',\n", " 'markdown_phpextra',\n", " 'markdown_strict',\n", " 'mediawiki',\n", " 'native',\n", " 'odt',\n", " 'opendocument',\n", " 'opml',\n", " 'org',\n", " 'pdf',\n", " 'plain',\n", " 'revealjs',\n", " 'rst',\n", " 'rtf',\n", " 's5',\n", " 'slideous',\n", " 'slidy',\n", " 'texinfo',\n", " 'textile'])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pypandoc.get_pandoc_formats()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
index
88602male27.00013.00SSecondmanTrueNaNSouthamptonnoTrue
88711female19.00030.00SFirstwomanFalseBSouthamptonyesTrue
88803femaleNaN1223.45SThirdwomanFalseNaNSouthamptonnoFalse
88911male26.00030.00CFirstmanTrueCCherbourgyesTrue
89003male32.0007.75QThirdmanTrueNaNQueenstownnoTrue
\n" ] }, { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
survivedpclasssexagesibspparchfareembarkedclasswhoadult_maledeckembark_townalivealone
index
88602male27.00013.00SSecondmanTrueNaNSouthamptonnoTrue
88711female19.00030.00SFirstwomanFalseBSouthamptonyesTrue
88803femaleNaN1223.45SThirdwomanFalseNaNSouthamptonnoFalse
88911male26.00030.00CFirstmanTrueCCherbourgyesTrue
89003male32.0007.75QThirdmanTrueNaNQueenstownnoTrue
\n", "
" ], "text/plain": [ " survived pclass sex age sibsp parch fare embarked class \\\n", "index \n", "886 0 2 male 27.0 0 0 13.00 S Second \n", "887 1 1 female 19.0 0 0 30.00 S First \n", "888 0 3 female NaN 1 2 23.45 S Third \n", "889 1 1 male 26.0 0 0 30.00 C First \n", "890 0 3 male 32.0 0 0 7.75 Q Third \n", "\n", " who adult_male deck embark_town alive alone \n", "index \n", "886 man True NaN Southampton no True \n", "887 woman False B Southampton yes True \n", "888 woman False NaN Southampton no False \n", "889 man True C Cherbourg yes True \n", "890 man True NaN Queenstown no True " ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "titanic_tail = titanic.tail()\n", "\n", "pypandoc.convert(titanic_tail.to_html(), \"markdown\", format=\"html\", outputfile=\"titanic_tail.md\")\n", "pypandoc.convert(titanic_tail.to_html(), \"markdown_github\", format=\"html\", outputfile=\"titanic_tail_github.md\")\n", "pypandoc.convert(titanic_tail.to_html(), \"markdown_mmd\", format=\"html\", outputfile=\"titanic_tail_mmd.md\")\n", "pypandoc.convert(titanic_tail.to_html(), \"markdown_phpextra\", format=\"html\", outputfile=\"titanic_tail_phpex.md\")\n", "\n", "pypandoc.convert(titanic_tail.to_html(index=False), \"markdown_github\", format=\"html\", outputfile=\"titanic_tail_github_noindex.md\")\n", "\n", "titanic_tail.index.names = [\"index\"]\n", "pypandoc.convert(titanic_tail.to_html(), \"markdown_github\", format=\"html\", outputfile=\"titanic_tail_github_index.md\")\n", "\n", "print(titanic_tail.to_html())\n", "titanic_tail\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import tabulate" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone |\n", "|----:|-----------:|---------:|:-------|------:|--------:|--------:|-------:|:-----------|:--------|:------|-------------:|:-------|:--------------|:--------|--------:|\n", "| 886 | 0 | 2 | male | 27 | 0 | 0 | 13 | S | Second | man | 1 | nan | Southampton | no | 1 |\n", "| 887 | 1 | 1 | female | 19 | 0 | 0 | 30 | S | First | woman | 0 | B | Southampton | yes | 1 |\n", "| 888 | 0 | 3 | female | nan | 1 | 2 | 23.45 | S | Third | woman | 0 | nan | Southampton | no | 0 |\n", "| 889 | 1 | 1 | male | 26 | 0 | 0 | 30 | C | First | man | 1 | C | Cherbourg | yes | 1 |\n", "| 890 | 0 | 3 | male | 32 | 0 | 0 | 7.75 | Q | Third | man | 1 | nan | Queenstown | no | 1 |\n", "| index | survived | pclass | sex | age | sibsp | parch | fare | embarked | class | who | adult_male | deck | embark_town | alive | alone |\n", "|--------:|-----------:|---------:|:-------|------:|--------:|--------:|-------:|:-----------|:--------|:------|-------------:|:-------|:--------------|:--------|--------:|\n", "| 886 | 0 | 2 | male | 27 | 0 | 0 | 13 | S | Second | man | 1 | nan | Southampton | no | 1 |\n", "| 887 | 1 | 1 | female | 19 | 0 | 0 | 30 | S | First | woman | 0 | B | Southampton | yes | 1 |\n", "| 888 | 0 | 3 | female | nan | 1 | 2 | 23.45 | S | Third | woman | 0 | nan | Southampton | no | 0 |\n", "| 889 | 1 | 1 | male | 26 | 0 | 0 | 30 | C | First | man | 1 | C | Cherbourg | yes | 1 |\n", "| 890 | 0 | 3 | male | 32 | 0 | 0 | 7.75 | Q | Third | man | 1 | nan | Queenstown | no | 1 |\n", "| | sex | counts |\n", "|---:|:-------|---------:|\n", "| 0 | male | 577 |\n", "| 1 | female | 314 |\n", "| | Third | First | Second |\n", "|:-------|--------:|--------:|---------:|\n", "| female | 94 | 76 | 144 |\n", "| male | 122 | 108 | 347 |\n" ] } ], "source": [ "titanic_tail = titanic.tail()\n", "\n", "print(tabulate.tabulate(titanic_tail, list(titanic_tail.columns), tablefmt=\"pipe\"))\n", "\n", "#titanic_tail.index.name = \"index\"\n", "print(tabulate.tabulate(titanic_tail, [\"index\"] + list(titanic_tail.columns), tablefmt=\"pipe\"))\n", "\n", "titanic_group = titanic.sex.value_counts()\n", "print(tabulate.tabulate(titanic_group.reset_index(), [\"sex\", \"counts\"], tablefmt=\"pipe\"))\n", "\n", "titanic_crosstab = pd.crosstab(titanic.sex, titanic[\"class\"])\n", "print(tabulate.tabulate(titanic_crosstab, list(titanic[\"class\"].unique()), tablefmt=\"pipe\"))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "toc": { "toc_cell": false, "toc_number_sections": true, "toc_threshold": 6, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 0 }