subtotals groups Pandas DataFrames and creates subtotals at each level.
Install
python3 -m pip install subtotals
subtotals requires Python 3 or above.
Usage
If you have a sales.csv like this:
country city product sales growth
India Hyderabad Biscuit 866.1 -0.270
India Hyderabad Cake 26.4 -0.242
India Hyderabad Cream 38.3 -0.291
India Hyderabad Eggs 513.7 -0.113
India Bangalore Biscuit 41.9 -0.402
India Bangalore Cake 52.2 0.064
India Bangalore Cream 17.8 -0.052
India Bangalore Eggs 178.9 -0.261
India Coimbatore Biscuit 217.4 0.114
India Coimbatore Cake
India Coimbatore Cream 94.4 -0.288
India Coimbatore Eggs 72.8 -0.066
Singapore Singapore Biscuit 671.0 -0.014
Singapore Singapore Cake 560.2 -0.197
Singapore Singapore Cream 237.9 0.194
Singapore Singapore Eggs 719.0 0.118
USA South Plainfield Biscuit 18.3 -0.154
USA South Plainfield Cake 41.6 0.043
USA South Plainfield Cream 32.4 0.068
USA South Plainfield Eggs 12.5 0.084
USA Newport Beach Biscuit 1352.4 0.384
USA Newport Beach Cake 190.2 0.119
USA Newport Beach Cream 148.2 0.053
USA Newport Beach Eggs
... then:
subtotals.groupby(
data=pd.read_csv('tests/sales.csv'),
by=['country', 'city'],
agg={'sales': 'sum', 'growth': 'max'}
)
... looks like this:
level country city sales growth
0 Total Total 6103.6 0.384 <- grand total
1 India Total 2119.9 0.114 <- sub total by country
1 Singapore Total 2188.1 0.194
1 USA Total 1795.6 0.384
2 India Hyderabad 1444.5 -0.113 <- sub sub total by city
2 India Bangalore 290.8 0.064
2 India Coimbatore 384.6 0.114
2 Singapore Singapore 2188.1 0.194
2 USA South Plainfield 104.8 0.084
2 USA Newport Beach 1690.8 0.384
- The first row has the grand total of all metrics.
- The next set of rows have the sub total by the first group (country)
- The next set of rows have the sub sub total by the second
groupbyelements, and so on.
Total rows are labels by margins_name which defaults to Total.
API
subtotals.groupby returns a subtotal-ed DataFrame and accepts:
data(pd.DataFrame): The data to group and subtotalgroups(list of column names): Levels by which the data must be groupedagg: How to aggregated columns. Same as Pandas GroupBy.aggmargins_name(str): Name of the row that contains the subtotals
Metadata
Release files for subtotals 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| subtotals-0.1.2.tar.gz | 4.8 kB | Without provenance | Details for subtotals-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| subtotals-0.1.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for subtotals-0.1.2-py2.py3-none-any.whl |
Total release size: 8.4 kB
Release files / subtotals-0.1.2.tar.gz
| Download URL | subtotals-0.1.2.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
797b9243b23beb60582cfe828ab90b1d998dd9b09755b02bcd744940b8e104be
|
|
BLAKE2b-256 checksum How to use checksums |
dcba7d9d247e57ebdb15f42c55a5a382ee401f32a0f0c21e289c1565d9051385
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Release files / subtotals-0.1.2-py2.py3-none-any.whl
| Download URL | subtotals-0.1.2-py2.py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
bef4986d83d437674e81aeeaa54f48747848164966290bcebfb36bdcd48deeb5
|
|
BLAKE2b-256 checksum How to use checksums |
f0ff31fdd893e9403a08faeba79c3c2b6aab9866f82d348619fd7362ec523a5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Security for subtotals 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.