Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
  • Cookbook

    • Cookbook
    • Setup
    • Help
    • System
    • Parsing
    • Foreign Shell Scripts
    • Pattern Matching
    • Custom Completers
    • External Completers
    • Module Scenarios
    • Files
    • Git
    • Parsing Git Log
    • Acting on keypresses using `input listen`
    • HTTP
    • Direnv
    • ssh-agent
    • Advanced table workflows
    • Polars vs Pandas vs Nushell
    • jq vs Nushell

Polars vs Pandas vs Nushell

A dataframe example based on https://studioterabyte.nl/en/blog/polars-vs-pandas

1. Opening the file and show the shape of the DataFrame

let df = polars open NYCTaxi.csv
$df | polars shape
# => ╭───┬─────────┬─────────╮
# => │ # │  rows   │ columns │
# => ├───┼─────────┼─────────┤
# => │ 0 │ 1458644 │      11 │
# => ╰───┴─────────┴─────────╯

2. Opening the file and show the first 5 rows

$df | polars first 5 | 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            │          455 │
# => │   │           │           │ 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 │
# => │   │           │           │ 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 │
# => │   │           │           │ 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 │
# => │   │           │           │ 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 │
# => │   │           │           │ 13:30:55      │ 13:38:10      │               │               │               │               │               │              │              │
# => ╰───┴───────────┴───────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴──────────────┴──────────────╯

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

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

$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

$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      │               │               │               │               │               │              │              │
# => │ 1 │ id2377394 │         1 │ 2016-06-12    │ 2016-06-12    │             1 │        -73.98 │         40.74 │        -74.00 │         40.73 │ N            │        11.05 │
# => │   │           │           │ 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            │        35.40 │
# => │   │           │           │ 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            │         7.15 │
# => │   │           │           │ 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            │         7.25 │
# => │   │           │           │ 13:30:55      │ 13:38:10      │               │               │               │               │               │              │              │
# => ╰───┴───────────┴───────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┴──────────────┴──────────────╯

5. Opening the file and filtering out all rows with a trip duration shorther than 500 seconds

$df | polars filter-with ((polars col trip_duration) >= 500) | polars first 5 | 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 │ 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

$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 │
# => ╰───┴───────────┴───────────────╯
Edit this page on GitHub
Contributors: fdncred, Ifthel, Justin Ma, arnau, Jan Klass
Prev
Advanced table workflows
Next
jq vs Nushell