Home Explore Blog CI



nushell

3rd chunk of `cookbook/polars_v_pandas_v_nushell.md`
5706d8368f7debee72d13a047029d6cec01f9a6061ea261a0000000100001860
$df | polars with-column (polars col id | polars str-lengths | polars as vendor_id_lengths) | polars first 5 | polars collect
# => ╭───┬───────────┬───────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────╮
# => │ # │    id     │ vendor_id │ pickup_datet │ dropoff_date │ passenger_c │ pickup_long │ pickup_lati │ dropoff_lon │ dropoff_lat │ store_and_f │ trip_durati │ vendor_id_l │
# => │   │           │           │ ime          │ time         │ ount        │ itude       │ tude        │ gitude      │ itude       │ wd_flag     │ on          │ ength       │
# => ├───┼───────────┼───────────┼──────────────┼──────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14   │ 2016-03-14   │           1 │      -73.98 │       40.77 │      -73.96 │       40.77 │ N           │         455 │           9 │
# => │   │           │           │ 17:24:55     │ 17:32:30     │             │             │             │             │             │             │             │             │
# => │ 1 │ id2377394 │         1 │ 2016-06-12   │ 2016-06-12   │           1 │      -73.98 │       40.74 │      -74.00 │       40.73 │ N           │         663 │           9 │
# => │   │           │           │ 00:43:35     │ 00:54:38     │             │             │             │             │             │             │             │             │
# => │ 2 │ id3858529 │         2 │ 2016-01-19   │ 2016-01-19   │           1 │      -73.98 │       40.76 │      -74.01 │       40.71 │ N           │        2124 │           9 │
# => │   │           │           │ 11:35:24     │ 12:10:48     │             │             │             │             │             │             │             │             │
# => │ 3 │ id3504673 │         2 │ 2016-04-06   │ 2016-04-06   │           1 │      -74.01 │       40.72 │      -74.01 │       40.71 │ N           │         429 │           9 │
# => │   │           │           │ 19:32:31     │ 19:39:40     │             │             │             │             │             │             │             │             │
# => │ 4 │ id2181028 │         2 │ 2016-03-26   │ 2016-03-26   │           1 │      -73.97 │       40.79 │      -73.97 │       40.78 │ N           │         435 │           9 │
# => │   │           │           │ 13:30:55     │ 13:38:10     │             │             │             │             │             │             │             │             │
# => ╰───┴───────────┴───────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────╯
```

## 4. Opening the file and apply a function to the "trip_duration" to divide the number by 60 to go from the second value to a minute value

```nu
$df | polars first 5 | polars with-column ((polars col trip_duration) / 60.0) | polars collect
# => ╭───┬───────────┬───────────┬───────────────┬───────────────┬───────────────┬───────────────┬───────────────┬───────────────┬───────────────┬──────────────┬──────────────╮
# => │ # │    id     │ vendor_id │ pickup_dateti │ dropoff_datet │ passenger_cou │ pickup_longit │ pickup_latitu │ dropoff_longi │ dropoff_latit │ store_and_fw │ trip_duratio │
# => │   │           │           │ me            │ ime           │ nt            │ ude           │ de            │ tude          │ ude           │ d_flag       │ n            │
# => ├───┼───────────┼───────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼───────────────┼──────────────┼──────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14    │ 2016-03-14    │             1 │        -73.98 │         40.77 │        -73.96 │         40.77 │ N            │         7.58 │
# => │   │           │           │ 17:24:55      │ 17:32:30      │               │               │               │               │               │              │              │

Title: Converting Trip Duration to Minutes in Polars with Nushell
Summary
This section demonstrates how to apply a mathematical operation to a column in a Polars DataFrame using Nushell. Specifically, it shows how to divide the 'trip_duration' column by 60.0 to convert the duration from seconds to minutes. The `with-column` command is used to perform the calculation and update the DataFrame. The `first 5` and `collect` commands are used to display the first 5 rows of the modified DataFrame.