Giter VIP home page Giter VIP logo

summarytools-python's Introduction

A Python implementation of the summarytools R package

This is going to be a slow developping project because 1) I'm a bit rusty in Python and 2) I don't have much free time. So if you'd like to contribute by all means do!

To see what summarytools is all about, see summarytools: An R Package For Descriptive Statistics

How to install

Just copy the summarytools.py file in your Python/Lib/site-packages folder.

Frequency tables with freq

First, a quick setup

import pandas as pd
from summarytools import freq

Import a sample dataset:

link = "https://raw.githubusercontent.com/dcomtois/summarytools-python/master/data/tobacco.csv"
tobacco = pd.read_csv(link)

Bare-bones example

freq(tobacco.age_gr)
         Freq    % Valid    % Valid Cum.    % Total    % Total Cum.
-----  ------  ---------  --------------  ---------  --------------
18-34     258      26.46           26.46      25.80           25.80
35-50     241      24.72           51.18      24.10           49.90
51-70     317      32.51           83.69      31.70           81.60
71 +      159      16.31          100.00      15.90           97.50
NaN        25                                  2.50          100.00
Total    1000     100.00          100.00     100.00          100.00

The returned object is a pandas Dataframe, and it is displayed using the tabulate library.

Markdown formatting

When switching format to 'pipe', we get pretty formatted markdown tables where mardown is supported, such as here on GitHub.

freq(tobacco.gender, format = 'pipe')
|       |   Freq |   % Valid |   % Valid Cum. |   % Total |   % Total Cum. |
|:------|-------:|----------:|---------------:|----------:|---------------:|
| F     |    489 |     50.00 |          50.00 |     48.90 |          48.90 |
| M     |    489 |     50.00 |         100.00 |     48.90 |          97.80 |
| NaN   |     22 |           |                |      2.20 |         100.00 |
| Total |    978 |    100.00 |         100.00 |    100.00 |         100.00 |

Fancier console display

When working in a regular Python console or in IPython, the 'fancy_grid' format from tabular produces good results:

freq(tobacco.gender, format = 'fancy_grid')
╒═══════╤════════╤═══════════╤════════════════╤═══════════╤════════════════╕
│       │   Freq │   % Valid │   % Valid Cum. │   % Total │   % Total Cum. │
╞═══════╪════════╪═══════════╪════════════════╪═══════════╪════════════════╡
│ F     │    489 │     50.00 │          50.00 │     48.90 │          48.90 │
├───────┼────────┼───────────┼────────────────┼───────────┼────────────────┤
│ M     │    489 │     50.00 │         100.00 │     48.90 │          97.80 │
├───────┼────────┼───────────┼────────────────┼───────────┼────────────────┤
│ NaN   │     22 │           │                │      2.20 │         100.00 │
├───────┼────────┼───────────┼────────────────┼───────────┼────────────────┤
│ Total │   1000 │    100.00 │         100.00 │    100.00 │         100.00 │
╘═══════╧════════╧═══════════╧════════════════╧═══════════╧════════════════╛

Parameters

For now, only a few parameters are implemented. We'll store a frequency table to show how we can tweak the display after its creation, using the print() method.

ft = freq(tobacco.gender, format = 'pipe')

Omit the Total row

ft.print(totals=False)
Freq % Valid % Valid Cum. % Total % Total Cum.
F 489 50.00 50.00 48.90 48.90
M 489 50.00 100.00 48.90 97.80
NaN 22 2.20 100.00

Omit missing data reporting

ft.print(nans=False)
Freq % Valid % Valid Cum.
F 489 50.00 50.00
M 489 50.00 100.00
Total 978 100.00 100.00

Control number of decimals to show

ft.print(digits=1)
Freq % Valid % Valid Cum. % Total % Total Cum.
F 489 50.0 50.0 48.9 48.9
M 489 50.0 100.0 48.9 97.8
NaN 22 2.2 100.0
Total 978 100.0 100.0 100.0 100.0

More to come... In the meantime feel free to chip in and contribute!

summarytools-python's People

Contributors

dcomtois avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.