Home Explore Blog CI



nushell

6th chunk of `cookbook/polars_v_pandas_v_nushell.md`
28e337b3dbf7468daba9c0ce962e278c7e62a2eccec23d8c0000000100001078
# => │   │           │           │ me            │ ime           │ nt            │ ude           │ de            │ tude          │ ude           │ d_flag       │ n            │
# => ├───┼───────────┼───────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼──────────────┼──────────────┤
# => │ 0 │ id2377394 │         1 │ 2016-06-12    │ 2016-06-12    │             1 │        -73.98 │         40.74 │        -74.00 │         40.73 │ N            │          663 │
# => │   │           │           │ 00:43:35      │ 00:54:38      │               │               │               │               │               │              │              │
# => │ 1 │ id3858529 │         2 │ 2016-01-19    │ 2016-01-19    │             1 │        -73.98 │         40.76 │        -74.01 │         40.71 │ N            │         2124 │
# => │   │           │           │ 11:35:24      │ 12:10:48      │               │               │               │               │               │              │              │
# => │ 2 │ id1324603 │         2 │ 2016-05-21    │ 2016-05-21    │             1 │        -73.97 │         40.80 │        -73.92 │         40.76 │ N            │         1551 │
# => │   │           │           │ 07:54:58      │ 08:20:49      │               │               │               │               │               │              │              │
# => │ 3 │ id0012891 │         2 │ 2016-03-10    │ 2016-03-10    │             1 │        -73.98 │         40.74 │        -73.97 │         40.79 │ N            │         1225 │
# => │   │           │           │ 21:45:01      │ 22:05:26      │               │               │               │               │               │              │              │
# => │ 4 │ id1436371 │         2 │ 2016-05-10    │ 2016-05-10    │             1 │        -73.98 │         40.76 │        -74.00 │         40.73 │ N            │         1274 │
# => │   │           │           │ 22:08:41      │ 22:29:55      │               │               │               │               │               │              │              │
# => ╰───┴───────────┴───────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴──────────────┴──────────────╯
```

## 6. Opening the file, filtering out all the rows with a "Y" store_and_fwd_flag value, group by ID and calculate the mean duration time

```nu
$df | polars filter-with ((polars col store_and_fwd_flag) == "N") | polars group-by id | polars agg (polars col trip_duration | polars mean) | polars sort-by id | polars first 5 | polars collect
# => ╭───┬───────────┬───────────────╮
# => │ # │    id     │ trip_duration │
# => ├───┼───────────┼───────────────┤
# => │ 0 │ id0000001 │       1105.00 │
# => │ 1 │ id0000003 │       1046.00 │
# => │ 2 │ id0000005 │        368.00 │
# => │ 3 │ id0000008 │        303.00 │
# => │ 4 │ id0000009 │        547.00 │
# => ╰───┴───────────┴───────────────╯
```

Title: Filtering and Grouping Polars DataFrame: Calculating Mean Trip Duration
Summary
This section demonstrates filtering a Polars DataFrame where the 'store_and_fwd_flag' is 'N', grouping the data by 'id', calculating the mean 'trip_duration' for each 'id', sorting by 'id', and then displaying the first 5 rows. The resulting table shows the 'id' and the corresponding mean 'trip_duration'.