Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flatbread

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flatbread

Pivot tables and graphs for pandas

  • 0.0.9
  • PyPI
  • Socket score

Maintainers
1

Flatbread

About

Flatbread is a small library which extends the pivot table functionality in pandas. Flatbread is accessible through the DataFrame using the pita accessor.

The library contains functions which will allow you to easily add totals/subtotals to one or more axes/levels of your pivot table. Furthermore, flatbread can calculate percentages from the totals/subtotals of each axis/level of your pivot table. You can transform the existing values in your table into percentages, but you can also add the percentages neatly next to your data. If the required (sub)totals are not present, then flatbread will add them automatically in order to perform the calculations. By default the (sub)totals are kept but you can drop them too. The library also contains some functionality built on top of matplotlib for plotting your data.

Name

Initially I planned for this library to be called pita -- short for pivot tables. But as that name was already taken on pypi.org the choice fell on flatbread.

Install

To install:

pip install flatbread

Pivot tables

Let's create a df for testing:

from random import randint
import pandas as pd
import flatbread as fb

df = pd._testing.makeCustomDataframe(
    nrows=8,
    ncols=4,
    data_gen_f=lambda r,c:randint(1,100),
    c_idx_nlevels=2,
    r_idx_nlevels=3,
    c_ndupe_l=[2,1],
    r_ndupe_l=[4,2,1],
)

Totals and subtotals

Flatbread let's you easily add subtotals to your pivot tables. Here we add totals and subtotals to both axes at once:

df.pipe(fb.totals.add, axis=2, level=[0,1])
C0C_l0_g0C_l0_g1Total
C1C_l1_g0C_l1_g1SubtotalC_l1_g2C_l1_g3Subtotal
R0R1R2
R_l0_g0R_l1_g0R_l2_g087684325587171
R_l2_g1217596156782178
R_l1_g1R_l2_g26684150384078228
R_l2_g38394177573188265
Subtotal178329507142193335842
R_l0_g1R_l1_g2R_l2_g432821145587142256
R_l2_g568229010070170260
R_l1_g3R_l2_g6552580402464144
R_l2_g71280923179110202
Subtotal167209376226260486862
Total3455388833684538211704

Percentages from totals and subtotals

Flatbread let's you calculate the percentages of the totals or subtotals. You can either transform the data itself or add the percentages into your pivot table as separate columns. When rounding the percentages they will always add up to 100%:

df.pipe(fb.percs.add, level=1)
C0C_l0_g0C_l0_g1
C1C_l1_g0C_l1_g1C_l1_g2C_l1_g3
abs%abs%abs%abs%
R0R1R2
R_l0_g0R_l1_g0R_l2_g084.57623.13222.55528.5
R_l2_g12111.87522.81510.66734.7
R_l1_g1R_l2_g26637.18425.53826.84020.7
R_l2_g38346.69428.65740.13116.1
Subtotal178100.0329100.0142100.0193100.0
R_l0_g1R_l1_g2R_l2_g43219.28239.25524.38733.5
R_l2_g56840.72210.610044.37026.9
R_l1_g3R_l2_g65532.92511.94017.7249.2
R_l2_g7127.28038.33113.77930.4
Subtotal167100.0209100.0226100.0260100.0

Localize your table formats

Flatbread provides the format function for rendering your pivot table according to your preferred locale. Here we use nl-NL as an example:

df = pd._testing.makeCustomDataframe(
    nrows=5,
    ncols=4,
    data_gen_f=lambda r,c:randint(10,1000),
)

df.pipe(fb.percs.add).pipe(fb.format)
C0C_l0_g0C_l0_g1C_l0_g2C_l0_g3
abs%abs%abs%abs%
R0
R_l0_g070223,8571,757923,290839,6
R_l0_g179126,883925,668727,633314,5
R_l0_g257919,677723,763325,455324,2
R_l0_g357119,369921,31084,443919,1
R_l0_g431010,590827,748419,4592,6
Total2.953100,03.280100,02.491100,02.292100,0

Easy configuration

Flatbread let's you control most of its behavior through key-word arguments, but it is also easy to store your settings and use them globally throughout a project:

from flatbread import CONFIG

# pick your preferred locale and set it (used with `format`)
CONFIG.format['locale'] = 'nl_NL'
CONFIG.set_locale()

# set your own labels
CONFIG.aggregation['totals_name'] = 'Totes'
CONFIG.aggregation['label_rel'] = 'pct'

# define the number of digits to round to (-1 is no rounding)
CONFIG.aggregation['ndigits] = 2

# store your configuration permanently (across sessions)
CONFIG.save()

# restore your settings to 'factory' defaults
CONFIG.factory_reset()

Pivot charts

Use the Trendline object to create trendlines. Compare multiple years:

tl = fb.TrendLine.from_df(
    sample,
    offset_year = 2019,
    datefield   = 'Date of Application',
    yearfield   = 'Academic Year',
    period      = 'w',
    end         = '2019-09-01',
    grouper     = 'Academic Year',
    focus       = 2019,
)

fig = tl.plot()

Split your graphs in rows and columns:

tl = fb.TrendLine.from_df(
    sample,
    offset_year = 2019,
    datefield   = 'Date Processed',
    yearfield   = 'Academic Year',
    period      = 'w',
    end         = '2019-10-01',
    grouper     = 'Faculty',
    focus       = 'Humanities',
)

fig = tl.plot(
    rows   = 'Origin Country',
    cols   = 'Examination Type',
    cum    = True,
    filter = "`Academic Year` == 2019"
)

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc