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. VerticaPy 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 verticapy as vp

help(vp.vDataFrame.agg)
Help on function aggregate in module verticapy.core.vdataframe._aggregate:

aggregate(self, func: Annotated[Union[str, list[str], ForwardRef('StringSQL'), list['StringSQL']], ''], columns: Optional[Annotated[Union[str, list[str]], 'STRING representing one column or a list of columns']] = None, ncols_block: int = 20, processes: int = 1) -> verticapy.core.tablesample.base.TableSample

Aggregates the vDataFrame using the input functions.

Parameters
----------
func: SQLExpression
    List of the different aggregations:

     - aad:
        average absolute deviation.
     - approx_median:
        approximate median.
     - approx_q%:
        approximate q quantile (ex: approx_50%
        for the approximate median).
     - approx_unique:
        approximative cardinality.
     - count:
        number of non-missing elements.
     - cvar:
        conditional value at risk.
     - dtype:
        virtual column type.
     - iqr:
        interquartile range.
     - kurtosis:
        kurtosis.
     - jb:
        Jarque-Bera index.
     - mad:
        median absolute deviation.
     - max:
        maximum.
     - mean:
        average.
     - median:
        median.
     - min:
        minimum.
     - mode:
        most occurent element.
     - percent:
        percent of non-missing elements.
     - q%:
        q quantile (ex: 50% for the median)
        Use the ``approx_q%`` (approximate quantile)
        aggregation to get better performance.
     - prod:
        product.
     - range:
        difference between the max and the min.
     - sem:
        standard error of the mean.
     - skewness:
        skewness.
     - sum:
        sum.
     - std:
        standard deviation.
     - topk:
        kth most occurent element (ex: top1 for the mode)
     - topk_percent:
        kth most occurent element density.
     - unique:
        cardinality (count distinct).
     - var:
        variance.

    Other aggregations will work if supported by your database
    version.

columns: SQLColumns, optional
    List of  the vDataColumn's names. If empty,  depending on the
    aggregations, all or only numerical vDataColumns are used.
ncols_block: int, optional
    Number  of columns  used per query.  Setting  this  parameter
    divides  what  would otherwise be  one large query into  many
    smaller  queries  called "blocks", whose size is determine by
    the size of ncols_block.
processes: int, optional
    Number  of child processes  to  create. Setting  this  with  the
    ncols_block  parameter lets you parallelize a  single query into
    many smaller  queries, where each child process creates  its own
    connection to the database and sends one query. This can improve
    query performance, but consumes  more resources. If processes is
    set to 1, the queries are sent iteratively from a single process.

Returns
-------
TableSample
    result.

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

vdf = vp.read_csv("churn.csv")
vdf.agg(func = ["min", "10%", "median", "90%", "max", "kurtosis", "unique"])
...
kurtosis
unique
"SeniorCitizen"...1.362595895793912
"Partner"...-1.99595342119472
"Dependents"...-1.23437805716952
"tenure"...-1.3873716359716973
"PhoneService"...5.438907555087062
"PaperlessBilling"...-1.859606185608842
"MonthlyCharges"...-1.257259694549511585
"TotalCharges"...-0.2317987608693626530
"Churn"...-0.8702113423319812

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

vdf.describe()
...
approx_75%
max
"SeniorCitizen"...0.01.0
"Partner"...1.01.0
"Dependents"...1.01.0
"tenure"...55.072.0
"PhoneService"...1.01.0
"PaperlessBilling"...1.01.0
"MonthlyCharges"...89.85118.75
"TotalCharges"...3799.478684.8
"Churn"...1.01.0
vdf.describe(method = "all")
...
Abc
"Contract"
Varchar(28)
100%
Abc
"PaymentMethod"
Varchar(50)
100%
dtype...varchar(28)varchar(50)
percent...100.0100.0
count...70437043
top...Month-to-monthElectronic check
top_percent...55.01933.579
avg...11.301150078091718.5702115575749
stddev...2.985058424526545.04042209794139
min...812
approx_25%...816
approx_50%...1416
approx_75%...1423
max...1425
range...613
empty...00
vdf.describe(method = "categorical")
...
top
top_percent
"customerID"...0002-ORFBO0.014
"gender"...Male50.476
"SeniorCitizen"...083.785
"Partner"...051.697
"Dependents"...070.041
"tenure"...18.704
"PhoneService"...190.317
"MultipleLines"...No48.133
"InternetService"...Fiber optic43.959
"OnlineSecurity"...No49.666
"OnlineBackup"...No43.845
"DeviceProtection"...No43.944
"TechSupport"...No49.311
"StreamingTV"...No39.898
"StreamingMovies"...No39.543
"Contract"...Month-to-month55.019
"PaperlessBilling"...159.222
"PaymentMethod"...Electronic check33.579
"MonthlyCharges"...20.050.866
"TotalCharges"...[null]0.156
"Churn"...073.463

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

vdf.avg()
avg
"SeniorCitizen"0.162146812437882
"Partner"0.483032798523357
"Dependents"0.299588243646173
"tenure"32.3711486582422
"PhoneService"0.903166264375977
"PaperlessBilling"0.592219224762175
"MonthlyCharges"64.7616924605991
"TotalCharges"2283.30044084187
"Churn"0.265369870793696

Or just the median of a specific column.

vdf["tenure"].median()
Out[3]: 29.0

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

vdf["tenure"].median(approx = False)
Out[4]: 29.0

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

# SQL way
vdf.groupby(
    [
        "gender",
        "Contract",
    ],
    [
        "AVG(DECODE(Churn, 'Yes', 1, 0)) AS Churn",
    ],
)
Abc
gender
Varchar(20)
100%
...
Abc
Contract
Varchar(28)
100%
123
Churn
Float(22)
100%
1Female...Month-to-month0.437402597402597
2Female...One year0.104456824512535
3Male...One year0.120529801324503
4Male...Two year0.0305882352941176
5Female...Two year0.0260355029585799
6Male...Month-to-month0.416923076923077
# Pythonic way
import verticapy.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(20)
100%
...
Abc
Contract
Varchar(28)
100%
123
max_tenure
Integer
100%
1Female...Month-to-month71
2Female...One year72
3Male...One year72
4Male...Two year72
5Female...Two year72
6Male...Month-to-month72

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.