Home Explore Blog CI



nushell

14th chunk of `book/dataframes.md`
45cc7502d15ae40f9e73264d943ddc1ba9049e7d8fb1f35f000000010000119c
# => ╰───┴───╯
```

Let's try something more complicated and create aggregations from a lazy
dataframe

```nu
let lf_1 =  [[name value]; [one 1] [two 2] [one 1] [two 3]] | polars into-lazy

$lf_1
| polars group-by name
| polars agg [
     (polars col value | polars sum | polars as sum)
     (polars col value | polars mean | polars as mean)
]
| polars collect
# => ╭───┬──────┬─────┬──────╮
# => │ # │ name │ sum │ mean │
# => ├───┼──────┼─────┼──────┤
# => │ 0 │ two  │   5 │ 2.50 │
# => │ 1 │ one  │   2 │ 1.00 │
# => ╰───┴──────┴─────┴──────╯
```

And we could join on a lazy dataframe that hasn't being collected. Let's join
the resulting group by to the original lazy frame

```nu
let lf_2 =  [[name value]; [one 1] [two 2] [one 1] [two 3]] | polars into-lazy
let group = $lf_2
    | polars group-by name
    | polars agg [
      (polars col value | polars sum | polars as sum)
      (polars col value | polars mean | polars as mean)
    ]

$lf_2 | polars join $group name name | polars collect
# => ╭───┬──────┬───────┬─────┬──────╮
# => │ # │ name │ value │ sum │ mean │
# => ├───┼──────┼───────┼─────┼──────┤
# => │ 0 │ one  │     1 │   2 │ 1.00 │
# => │ 1 │ two  │     2 │   5 │ 2.50 │
# => │ 2 │ one  │     1 │   2 │ 1.00 │
# => │ 3 │ two  │     3 │   5 │ 2.50 │
# => ╰───┴──────┴───────┴─────┴──────╯
```

As you can see lazy frames are a powerful construct that will let you query
data using a flexible syntax, resulting in blazing fast results.

## Dataframe Commands

So far we have seen quite a few operations that can be done using `DataFrame`s
commands. However, the commands we have used so far are not all the commands
available to work with data and be assured that there will be more as the
feature becomes more stable.

The next list shows the available dataframe commands with their descriptions, and
whenever possible, their analogous Nushell command.

::: warning
This list may be outdated. To get the up-to-date command list, see [Dataframe](/commands/categories/dataframe.md), [Lazyframe](/commands/categories/lazyframe.md), [Dataframe Or Lazyframe](/commands/categories/dataframe_or_lazyframe.md), [Expressions](/commands/categories/expression.html) command categories.
:::

<!-- This table was updated using the script from ../tools/dataframes_md-update.nu -->

| Command Name           | Applies To            | Description                                                                                      | Nushell Equivalent      |
| ---------------------- | --------------------- | ------------------------------------------------------------------------------------------------ | ----------------------- |
| polars agg             | dataframe             | Performs a series of aggregations from a group-by.                                               | math                    |
| polars agg-groups      | expression            | Creates an agg_groups expression.                                                                |                         |
| polars all-false       | dataframe             | Returns true if all values are false.                                                            |                         |
| polars all-true        | dataframe             | Returns true if all values are true.                                                             | all                     |
| polars append          | dataframe             | Appends a new dataframe.                                                                         |                         |
| polars arg-max         | dataframe             | Return index for max value in series.                                                            |                         |
| polars arg-min         | dataframe             | Return index for min value in series.                                                            |                         |
| polars arg-sort        | dataframe             | Returns indexes for a sorted series.                                                             |                         |

Title: Lazy DataFrames: Aggregations, Joins, and Available Commands
Summary
This section continues demonstrating aggregations and joins on lazy DataFrames and lists available DataFrame commands. It provides examples of grouping data with `polars group-by` and calculating aggregations like sum and mean using `polars agg`, joining the results to the original DataFrame. Then, it lists available dataframe commands along with their descriptions and nushell equivalents.