Package: pg-binny
pg_binnyDiscretize a whole dataframe into ≤N bins, using Top N categories.%nbdev_hideThediscretizefunction handles discrete & continuous columns:Continuous columns are cut intoNbins using supplied cutting function (defaults toqcutfor quantile cuts.Categorical columns: take the TopN-1, with the rest tossed into "Other"TODO:Describe and show the plot helpers too.Installconda install pg_binny-or-pip install pg_binny-or (locally)-pip install -e .(That's "pip install -edot")How to useMake a sample dataframe.importpandasaspdimportpg_binnyasbinnydataset='car_crashes'try:importseabornassnsdf=sns.load_dataset(dataset)exceptModuleNotFoundError:df=pd.read_csv(f'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/{dataset}.csv')df.sample(5)<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}</style>totalspeedingalcoholnot_distractedno_previousins_premiumins_lossesabbrev1915.15.7384.53013.13712.684661.8896.57ME1515.72.6693.92515.22913.659649.06114.47IA3514.13.9484.79413.95911.562697.73133.52OH5017.47.3085.56814.09415.660791.14122.04WY4319.47.7607.37217.65416.8781004.75156.83TXDiscretize with default binsdfd=binny.discretize(df)dfd.sample(5)---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-2-41b3e27056d4> in <module>
----> 1 dfd = binny.discretize(df)
      2 dfd.sample(5)
      3 


AttributeError: module 'pg_binny' has no attribute 'discretize'dfd['speeding'].dtypeCategoricalDtype(categories=[(1.7910000000000001, 2.413], (2.413, 3.496], (3.496, 3.948], (3.948, 4.095], (4.095, 4.608], (4.608, 5.032], (5.032, 6.014], (6.014, 6.923], (6.923, 7.76], (7.76, 9.45]],
, ordered=True)dfd['total'].dtypeCategoricalDtype(categories=[(5.899, 11.1], (11.1, 12.3], (12.3, 13.6], (13.6, 14.5], (14.5, 15.6], (15.6, 17.4], (17.4, 18.1], (18.1, 19.4], (19.4, 21.4], (21.4, 23.9]],
, ordered=True)You can set the #bins and the cutting function (defaults to quantile cut, but you may prefer plain-oldcut, or something else.?binny.discretize[0;31mSignature:[0m
[0mbinny[0m[0;34m.[0m[0mdiscretize[0m[0;34m([0m[0;34m[0m
[0;34m[0m    [0mdf[0m[0;34m,[0m[0;34m[0m
[0;34m[0m    [0mnbins[0m[0;34m=[0m[0;36m10[0m[0;34m,[0m[0;34m[0m
[0;34m[0m    [0mcut[0m[0;34m=[0m[0;34m<[0m[0mfunction[0m [0mqcut[0m [0mat[0m [0;36m0x7fae29d843b0[0m[0;34m>[0m[0;34m,[0m[0;34m[0m
[0;34m[0m    [0mverbose[0m[0;34m=[0m[0;36m2[0m[0;34m,[0m[0;34m[0m
[0;34m[0m    [0mdrop_useless[0m[0;34m=[0m[0;32mTrue[0m[0;34m,[0m[0;34m[0m
[0;34m[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;31mDocstring:[0m
Discretize columns in {df} to have at most {nbins} categories.
  * Categorical columns: take the Top n-1 plus "Other"
  * Continuous columns: cut into {nbins} using {cut}.

Returns a new discretized dataframe with the same column names.
Promotes discrete columns to categories.

Parameters
-----------
df: Dataframe to discretize
nbins: Max number of bins to use. May return fewer.
cut: Cutting method. Default `pd.qcut`. Consider pd.cut, or write your own.
verbose: 0: silent, 1: colnames, 2: (Default) top N for each column
drop_useless: Removes columns that have < 2 unique values.

Replaces numerical NA values with 'NA'.
[0;31mFile:[0m      /Volumes/Peregrine/binny/pg_binny/core.py
[0;31mType:[0m      functionOther functions[xforxindir(binny)ifx[:2]notin['__','pa','pd','rc']]['autolabel',
 'clean_category',
 'discretize',
 'drop_singletons',
 'is_numeric',
 'isnum']?binny.autolabel[0;31mSignature:[0m [0mbinny[0m[0;34m.[0m[0mautolabel[0m[0;34m([0m[0max[0m[0;34m,[0m [0mborder[0m[0;34m=[0m[0;32mFalse[0m[0;34m)[0m [0;34m->[0m [0;32mNone[0m[0;34m[0m[0;34m[0m[0m
[0;31mDocstring:[0m
Label bars in a barplot {ax} with their height.
Thanks to matplotlib, composition.ai, and jsoma/chart.py.

TODO: how to label with their legend labels?
[0;31mFile:[0m      /Volumes/Peregrine/binny/pg_binny/core.py
[0;31mType:[0m      function?binny.clean_category[0;31mSignature:[0m [0mbinny[0m[0;34m.[0m[0mclean_category[0m[0;34m([0m[0mdf[0m[0;34m,[0m [0mcol[0m[0;34m:[0m [0mstr[0m[0;34m)[0m [0;34m->[0m [0;32mNone[0m[0;34m[0m[0;34m[0m[0m
[0;31mDocstring:[0m
Remove unused categories from df.col, inplace.
If not a category, do nothing.
[0;31mFile:[0m      /Volumes/Peregrine/binny/pg_binny/core.py
[0;31mType:[0m      function?binny.is_numeric[0;31mSignature:[0m [0mbinny[0m[0;34m.[0m[0mis_numeric[0m[0;34m([0m[0mcol[0m[0;34m:[0m [0mstr[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m
[0;31mDocstring:[0m
Returns True iff already numeric, or can be coerced.
Usage: df.apply(is_numeric)
Usage: is_numeric(df['colname'])

Returns Boolean series.

From:
https://stackoverflow.com/questions/54426845/how-to-check-if-a-pandas-dataframe-contains-only-numeric-column-wise
[0;31mFile:[0m      /Volumes/Peregrine/binny/pg_binny/core.py
[0;31mType:[0m      functionHistorypg_binnyis an example extracting some frequently copy/pasted routines into a general purposenbdevproject.Originally calledbinnybecause it bins things, that was already taken on PyPi (for... a project that bins things).  The prefixpgis short for the project we were working on.The routines and text are completely general.