Aggregations#
When performing aggregations such as sum, mean and count, Daft enables you to group data by certain keys and aggregate within those keys.
Calling df.groupby() returns a GroupedDataFrame object which is a view of the original DataFrame but with additional context on which keys to group on. You can then call various aggregation methods to run the aggregation within each group, returning a new DataFrame.
GroupedDataFrame #
GroupedDataFrame(df: DataFrame, group_by: ExpressionsProjection)
Methods:
| Name | Description |
|---|---|
agg | Perform aggregations on this GroupedDataFrame. Allows for mixed aggregations. |
any_value | Returns an arbitrary value on this GroupedDataFrame. |
count | Performs grouped count on this GroupedDataFrame. |
count_distinct | Performs grouped count of distinct values on this GroupedDataFrame. |
list_agg | Performs grouped list on this GroupedDataFrame. |
list_agg_distinct | Performs grouped list distinct on this GroupedDataFrame (ignoring nulls). |
map_groups | Apply a user-defined function to each group. The name of the resultant column will default to the name of the first input column. |
max | Performs grouped max on this GroupedDataFrame. |
mean | Performs grouped mean on this GroupedDataFrame. |
min | Perform grouped min on this GroupedDataFrame. |
product | Performs grouped product on this GroupedDataFrame. |
skew | Performs grouped skew on this GroupedDataFrame. |
stddev | Performs grouped standard deviation on this GroupedDataFrame. |
string_agg | Performs grouped string concat on this GroupedDataFrame. |
sum | Perform grouped sum on this GroupedDataFrame. |
var | Performs grouped variance on this GroupedDataFrame. |
Attributes:
| Name | Type | Description |
|---|---|---|
df | DataFrame | |
group_by | ExpressionsProjection | |
agg #
agg(*to_agg: Expression | Iterable[Expression]) -> DataFrame
Perform aggregations on this GroupedDataFrame. Allows for mixed aggregations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*to_agg | Union[Expression, Iterable[Expression]] | aggregation expressions | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped aggregations |
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
╭────────┬─────────┬─────────┬────────┬────────╮
│ pet ┆ min_age ┆ max_age ┆ count ┆ name │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ String ┆ Int64 ┆ Int64 ┆ UInt64 ┆ String │
╞════════╪═════════╪═════════╪════════╪════════╡
│ cat ┆ 1 ┆ 4 ┆ 2 ┆ Alex │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ dog ┆ 2 ┆ 3 ┆ 2 ┆ Jordan │
╰────────┴─────────┴─────────┴────────┴────────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 | |
any_value #
any_value(*cols: ColumnInputType) -> DataFrame
Returns an arbitrary value on this GroupedDataFrame.
Values for each column are not guaranteed to be from the same row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to get | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with any values. |
Source code in daft/dataframe/dataframe.py
6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 | |
count #
count(*cols: ColumnInputType) -> DataFrame
Performs grouped count on this GroupedDataFrame.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped count per column. |
Source code in daft/dataframe/dataframe.py
6293 6294 6295 6296 6297 6298 6299 | |
count_distinct #
count_distinct(*cols: ColumnInputType) -> DataFrame
Performs grouped count of distinct values on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to count distinct values | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped count of distinct values per column. |
Examples:
1 2 3 4 5 | |
╭────────┬────────╮
│ keys ┆ vals │
│ --- ┆ --- │
│ String ┆ UInt64 │
╞════════╪════════╡
│ a ┆ 2 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ b ┆ 1 │
╰────────┴────────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 | |
list_agg #
list_agg(*cols: ColumnInputType) -> DataFrame
Performs grouped list on this GroupedDataFrame.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped list per column. |
Source code in daft/dataframe/dataframe.py
6369 6370 6371 6372 6373 6374 6375 | |
list_agg_distinct #
list_agg_distinct(*cols: ColumnInputType) -> DataFrame
Performs grouped list distinct on this GroupedDataFrame (ignoring nulls).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to form into a set | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped list distinct per column. |
Source code in daft/dataframe/dataframe.py
6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 | |
map_groups #
map_groups(udf: Expression) -> DataFrame
Apply a user-defined function to each group. The name of the resultant column will default to the name of the first input column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
udf | Expression | User-defined function to apply to each group. | required |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped aggregations |
Examples:
1 2 3 4 5 6 7 8 9 10 11 | |
╭────────┬────────────────────╮
│ group ┆ data │
│ --- ┆ --- │
│ String ┆ Float64 │
╞════════╪════════════════════╡
│ a ┆ 14.730919862656235 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b ┆ 331.62026476076517 │
╰────────┴────────────────────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 | |
max #
max(*cols: ColumnInputType) -> DataFrame
Performs grouped max on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to max | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped max. |
Source code in daft/dataframe/dataframe.py
6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 | |
mean #
mean(*cols: ColumnInputType) -> DataFrame
Performs grouped mean on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to mean | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped mean. |
Source code in daft/dataframe/dataframe.py
6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 | |
min #
min(*cols: ColumnInputType) -> DataFrame
Perform grouped min on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to min | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped min. |
Source code in daft/dataframe/dataframe.py
6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 | |
product #
product(*cols: ColumnInputType) -> DataFrame
Performs grouped product on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to product | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped products. |
Examples:
1 2 3 4 5 | |
╭────────┬───────╮
│ keys ┆ col_a │
│ --- ┆ --- │
│ String ┆ Int64 │
╞════════╪═══════╡
│ a ┆ 6 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ b ┆ 100 │
╰────────┴───────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 | |
skew #
skew(*cols: ColumnInputType) -> DataFrame
Performs grouped skew on this GroupedDataFrame.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with the grouped skew per column. |
Source code in daft/dataframe/dataframe.py
6301 6302 6303 6304 6305 6306 6307 | |
stddev #
stddev(*cols: ColumnInputType, ddof: int = 1) -> DataFrame
Performs grouped standard deviation on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to stddev | () |
ddof | int | Delta degrees of freedom used in the denominator | 1 |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped standard deviation. |
Examples:
1 2 3 4 5 | |
╭────────┬─────────╮
│ keys ┆ col_a │
│ --- ┆ --- │
│ String ┆ Float64 │
╞════════╪═════════╡
│ a ┆ 1 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ b ┆ None │
╰────────┴─────────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 | |
string_agg #
string_agg(*cols: ColumnInputType, delimiter: str | None = None) -> DataFrame
Performs grouped string concat on this GroupedDataFrame.
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped string concatenated per column. |
Source code in daft/dataframe/dataframe.py
6388 6389 6390 6391 6392 6393 6394 | |
sum #
sum(*cols: ColumnInputType) -> DataFrame
Perform grouped sum on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to sum | () |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped sums. |
Source code in daft/dataframe/dataframe.py
6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 | |
var #
var(*cols: ColumnInputType, ddof: int = 1) -> DataFrame
Performs grouped variance on this GroupedDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*cols | Union[str, Expression] | columns to compute variance for | () |
ddof | int | Delta degrees of freedom used in the denominator | 1 |
Returns:
| Name | Type | Description |
|---|---|---|
DataFrame | DataFrame | DataFrame with grouped variance. |
Examples:
1 2 3 4 5 | |
╭────────┬─────────╮
│ keys ┆ col_a │
│ --- ┆ --- │
│ String ┆ Float64 │
╞════════╪═════════╡
│ a ┆ 1 │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ b ┆ None │
╰────────┴─────────╯
(Showing first 2 of 2 rows) Source code in daft/dataframe/dataframe.py
6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 | |