Loading...

Descriptive Statistics

The easiest way to understand data is to aggregate it. An aggregation is a number or a category which summarizes the data. vastorbit lets you compute all well-known aggregation in a single line.

The aggregate() method is the best way to compute multiple aggregations on multiple columns at the same time.

import vastorbit as vo

help(vo.VastFrame.agg)

This is a tremendously useful function for understanding your data. Let’s use the churn dataset

vdf = vo.read_csv("churn.csv")
vdf.agg(func = ["min", "10%", "median", "90%", "max", "kurtosis", "unique"])
min10%median90%maxkurtosisunique
"seniorcitizen"0.00.00.01.01.01.36259589579392152.0
"tenure"0.02.029.069.072.0-1.387371635971702873.0
"monthlycharges"18.2520.10216282642089470.44862291192591102.58851812818602118.75-1.25725969454951141585.0
"totalcharges"18.887.111629123089321400.08162261609125978.3833310533538684.8-0.231798760869367466530.0

Some methods, like describe(), are abstractions of the aggregate() method; they simplify the call to computing specific aggregations.

vdf.describe()
countmeanstdminapprox_25%approx_50%approx_75%max
"seniorcitizen"7043.00.16214681243788160.3686116056100130.00.00.00.01.0
"tenure"7043.032.3711486582422324.559481023094460.09.029.055.072.0
"monthlycharges"7043.064.7616924605991930.09004709767849318.2535.6851469124879870.7247098144203489.8382428893593118.75
"totalcharges"7032.02283.30044084186562266.77136188314518.8404.42920315786281411.80101144467453762.5706482694768684.8
vdf.describe(method = "all")
"seniorcitizen""tenure""monthlycharges""totalcharges""customerid""gender""partner""dependents""phoneservice""multiplelines""internetservice""onlinesecurity""onlinebackup""deviceprotection""techsupport""streamingtv""streamingmovies""contract""paperlessbilling""paymentmethod""churn"
dtypeintegerintegerdoubledoublevarchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)varchar(50)
percent100.0100.0100.099.844100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0100.0
count704370437043703270437043704370437043704370437043704370437043704370437043704370437043
top0120.05[null]8205-VSLRBMaleNoNoYesNoFiber opticNoNoNoNoNoNoMonth-to-monthYesElectronic checkNo
top_percent83.7858.7040.8660.1560.01450.47651.69770.04190.31748.13343.95949.66643.84543.94449.31139.89839.54355.01959.22233.57973.463
avg0.162146812437881632.3711486582422364.761692460599192283.300440841865610.04.9904870083771122.48303279852335652.29958824364617342.90316626437597643.77750958398409786.3000141984949595.9700411756353836.0282550049694736.0272611103223065.9735907993752666.0677268209569786.07127644469686311.3011500780917232.59221922476217518.5702115575748972.265369870793696
stddev0.36861160561001324.5594810230944630.0900470976784932266.7713618831450.01.00002574729430260.499747510719987240.45811016751001530.295752231783634744.0303865263930354.1788180853609376.8665483144875326.8368143373328326.8373272350138456.8647530890959356.8162965912496246.8144372361839742.9850584245265450.49145692404940685.0404220979413980.4415613051219471
min0018.2518.81042222222222282122
approx_25%0935.68514691248798404.42920315786281042232322222282152
approx_50%02970.724709814420341411.801011444674510522333333333133162
approx_75%05589.83824288935933762.570648269476106333311444444143233
max172118.758684.81063331611191919191919143253
range172100.58666.00211114917171717171761131
empty[null][null][null][null]00000000000000000
vdf.describe(method = "categorical")
dtypecounttoptop_percent
"customerid"varchar(50)70438205-VSLRB0.014
"gender"varchar(50)7043Male50.476
"seniorcitizen"integer7043083.785
"partner"varchar(50)7043No51.697
"dependents"varchar(50)7043No70.041
"tenure"integer704318.704
"phoneservice"varchar(50)7043Yes90.317
"multiplelines"varchar(50)7043No48.133
"internetservice"varchar(50)7043Fiber optic43.959
"onlinesecurity"varchar(50)7043No49.666
"onlinebackup"varchar(50)7043No43.845
"deviceprotection"varchar(50)7043No43.944
"techsupport"varchar(50)7043No49.311
"streamingtv"varchar(50)7043No39.898
"streamingmovies"varchar(50)7043No39.543
"contract"varchar(50)7043Month-to-month55.019
"paperlessbilling"varchar(50)7043Yes59.222
"paymentmethod"varchar(50)7043Electronic check33.579
"monthlycharges"double704320.050.866
"totalcharges"double7032[null]0.156
"churn"varchar(50)7043No73.463

Multi-column aggregations can also be called with many built-in methods. For example, you can compute the VastFrameavg() of all the numerical columns in just one line.

vdf.avg()
avg
"seniorcitizen"0.1621468124378816
"tenure"32.37114865824223
"monthlycharges"64.76169246059919
"totalcharges"2283.3004408418656

Or just the median of a specific column.

vdf["tenure"].median()

The approximate median is automatically computed. Set the parameter approx to False to get the exact median.

vdf["tenure"].median(approx = False)

You can also use the groupby() method to compute customized aggregations.

# SQL way
vdf.groupby(
    [
        "gender",
        "Contract",
    ],
    [
        "AVG(CASE WHEN Churn = 'Yes' THEN 1 ELSE 0 END) AS Churn",
    ],
)
Abc
gender
Varchar(50)
100%
Abc
contract
Varchar(50)
100%
123
Churn
Double
100%
1MaleMonth-to-month0.4169230769230769
2FemaleOne year0.10445682451253482
3FemaleTwo year0.02603550295857988
4MaleOne year0.1205298013245033
5MaleTwo year0.03058823529411765
6FemaleMonth-to-month0.4374025974025974
# Pythonic way
import vastorbit.sql.functions as fun

vdf.groupby(
    [
        "gender",
        "Contract",
    ],
    [
        fun.min(vdf["tenure"])._as("min_tenure"),
        fun.max(vdf["tenure"])._as("max_tenure"),
    ],
)
Abc
gender
Varchar(50)
100%
Abc
contract
Varchar(50)
100%
123
min_tenure
Integer
100%
123
max_tenure
Integer
100%
1MaleMonth-to-month172
2FemaleMonth-to-month171
3FemaleOne year172
4MaleOne year072
5MaleTwo year072
6FemaleTwo year072

Computing many aggregations at the same time can be resource intensive. You can use the parameters ncols_block and processes to manage the ressources.

For example, the parameter ncols_block will divide the main query into smaller using a specific number of columns. The parameter processes allows you to manage the number of queries you want to send at the same time.

An entire example is available in the aggregate() documentation.