Home Explore Blog CI



nushell

2nd chunk of `cookbook/polars_v_pandas_v_nushell.md`
66e55dd46c211ba4ac76cf84c6ff2581babfa67e4c787ffc00000001000019d4
# => ╰───┴───────────┴───────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴──────────────┴──────────────╯
```

## 3. Opening the file and get the length of all strings in the "id" column

```nu
let ids = $df | polars first 5 | polars get id | polars str-lengths
$df | polars first 5 | polars append $ids | polars rename id_x vendor_id_length
# => ╭───┬───────────┬───────────┬──────────────┬──────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────┬─────────────╮
# => │ # │    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     │             │             │             │             │             │             │             │             │
# => ╰───┴───────────┴───────────┴──────────────┴──────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────┴─────────────╯
```

Here's an alternate approach using `with-column`

```nu
$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 │

Title: Calculating String Lengths in Polars with Nushell: Alternative Approaches
Summary
This section continues the demonstration of using Polars within Nushell to work with DataFrames. It focuses on calculating the string lengths of the 'id' column in a DataFrame. It shows two approaches: one using `append` and `rename`, and another, more concise approach using `with-column` to add a new column containing the calculated string lengths. Both methods achieve the same result, providing flexibility in how data manipulation is performed.