Package: zentables
ZenTables - Stress-Free Descriptive Tables in PythonZenTablestransforms yourpandasDataFrames into beautiful, publishable tables in one line of code, which you can then transfer into Google Docs and other word processors with one click. Supercharge your workflow when you are writing papers and reports.importzentablesaszendf.zen.pretty()FeaturesBeautiful tables in one lineGoogle Docs/Word ready in one clickDescriptive statistics at varying levels of aggregationControl table aesthetics globallyand many more to come....InstallationViapipfrom PyPI:pipinstallzentablesViapipfrom GitHub directlypipinstall-Ugit+https://github.com/thepolicylab/ZenTablesHow to useZenTables1. How to format anyDataFrameFirst, import the package alongsidepandas:importpandasaspdimportzentablesaszenThen, to format anyDataFrame, simply use:df.zen.pretty()And this is the result:Click on theCopy Tablebutton to transfer the table to Google Docs and Word. Formatting will be preserved.Results in Google Docs (Tested on Chrome, Firefox, and Safari):Results in Microsoft Word:2. How to control my tables' appearance?ZenTablesprovides two ways to control the aesthetics of the tables. You can use global settings to control the font and font size of the tables via:zen.set_options(font_family="Times New Roman, serif",font_size=12)Note:Whenfont_sizeis specified as anint, it will be interpreted as points (pt). All other CSS units are accepted as astr.Or you can override any global options by specifyingfont_familyandfont_sizeinzen.pretty()method:df.zen.pretty(font_family="Times New Roman, serif",font_size="12pt")Both will result in a table that looks like thisWe are working on adding more customization options in a future release.3. How to create common descriptive tables usingZenTables?3.1. Frequency TablesUsedf.zen.freq_tables()to create simple frequency tables:freq_table=df.zen.freq_table(index=["Segment","Region"],columns=["Category"],values="Order ID",props="index",totals=True,subtotals=True,totals_names="Total"subtotals_names="Subtotal",)freq_table.zen.pretty()# You can also chain the methodsUsepropsto control whether to add percentages of counts. Whenpropsis not set (the default), no percentages will be added. You can also specifypropsto calculate percentages over"index"(rows),"columns", or"all"(over the totals of the immediate top level).Usetotalsandsubtotalsparameters to specify whether totals and subtotals will be added. Note that whenpropsis notNone, bothtotalsandsubtotalswill beTrue, and whensubtotalsis set toTrue, this will also overridetotalssettings toTrue.Additionally, you can control the names of the total and subtotal categories usingtotals_namesandsubtotals_namesparameters.3.2. Mean and standard deviation tablesUsedf.zen.mean_sd_table()to create descriptives with n, mean, and standard deviation:mean_sd_table=df.zen.mean_sd_table(index=["Segment","Region"],columns=["Category"],values="Sales",margins=True,margins_name="All",submargins=True,submargins_name="All Regions",)mean_sd_table.zen.pretty()# You can also chain the methodsSimilar tofreq_tables, you can usemarginsandsubmarginsparameters to specify whether aggregations at the top and intermediate levels will be added. Additionally, you can control the names of the total and subtotal categories usingmargins_namesandsubmargins_namesparameters.3.3 Other descriptive statistics tablesFor all other types of tables,ZenTablesprovides its owndf.zen.pivot_table()method:mean_median_table=df.zen.pivot_table(index=["Segment","Region"],columns=["Category"],values="Sales",aggfunc=["count","mean","median"],margins=True,margins_name="All",submargins=True,submargins_name="All Regions",).rename(# rename columnscolumns={"count":"n","mean":"Mean","median":"Median",})mean_median_table.zen.pretty().format(precision=1)# Specify level of precisionThere are two differences between thispivot_table()method and thepandaspivot_tablemethod. First, likemean_sd_table(), it providessubmarginsandsubmargins_namesfor creating intermediate-level aggregations. Second, results are grouped byvalues,columns, andaggfuncs, instead ofaggfuncs,values, andcolumns. This provides more readability than what thepandasversion provides.4. Tips and tricksdf.zen.pretty()returns a subclass ofpandasStyler, which means you can chain all other methods afterdf.style.format()in the previous section is an example. For more formatting options, please seethis page inpandasdocumentationAll other methods inZenTablesreturns a regularDataFramethat can be modified further.The names of the index and columns are by default hidden. You can get them back by doing this:df.zen.pretty().show_index_names().show_column_names()You can also disable theCopy Tablebutton like this:df.zen.pretty().hide_copy_button()TODOMore tests on compatibility withStylerinpandasMore options for customizationA theming systemMore to come...ContributingContributions are welcome, and they are greatly appreciated! If you have a new idea for a simple table that we should add, please submit an issue.ContributorsPrincipally written by Paul Xu atThe Policy Lab. Other contributors:Kevin H. WilsonEdward HuhSpecial thanksAll the members ofThe Policy Labat Brown University for their feedbackThesidetablepackagefor ideas and inspiration.