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

The examples use NYCTaxi.csv, which is the train.csv file from the New York City Taxi Trip Duration dataset on Kaggle.

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_datetime   │  dropoff_datetime   │ passenger_count │ pickup_longitude │ pickup_latitude │ dropoff_longitude │ dropoff_latitude │ store_and_fwd_fla │ trip_durat │
# => │   │           │           │                     │                     │                 │                  │                 │                   │                  │ g                 │ ion        │
# => ├───┼───────────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┼──────────────────┼─────────────────┼───────────────────┼──────────────────┼───────────────────┼────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14 17:24:55 │ 2016-03-14 17:32:30 │               1 │           -73.98 │           40.77 │            -73.96 │            40.77 │ N                 │        455 │
# => │ 1 │ id2377394 │         1 │ 2016-06-12 00:43:35 │ 2016-06-12 00:54:38 │               1 │           -73.98 │           40.74 │            -74.00 │            40.73 │ N                 │        663 │
# => │ 2 │ id3858529 │         2 │ 2016-01-19 11:35:24 │ 2016-01-19 12:10:48 │               1 │           -73.98 │           40.76 │            -74.01 │            40.71 │ N                 │       2124 │
# => │ 3 │ id3504673 │         2 │ 2016-04-06 19:32:31 │ 2016-04-06 19:39:40 │               1 │           -74.01 │           40.72 │            -74.01 │            40.71 │ N                 │        429 │
# => │ 4 │ id2181028 │         2 │ 2016-03-26 13:30:55 │ 2016-03-26 13:38:10 │               1 │           -73.97 │           40.79 │            -73.97 │            40.78 │ N                 │        435 │
# => ╰───┴───────────┴───────────┴─────────────────────┴─────────────────────┴─────────────────┴──────────────────┴─────────────────┴───────────────────┴──────────────────┴───────────────────┴────────────╯

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_datetime   │  dropoff_datetime   │ passenger_count │ pickup_longitude │ pickup_latitude │ dropoff_longitude │ dropoff_la │ store_and_ │ trip_durat │ vendor_id_ │
# => │   │           │           │                     │                     │                 │                  │                 │                   │ titude     │ fwd_flag   │ ion        │ length     │
# => ├───┼───────────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┼──────────────────┼─────────────────┼───────────────────┼────────────┼────────────┼────────────┼────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14 17:24:55 │ 2016-03-14 17:32:30 │               1 │           -73.98 │           40.77 │            -73.96 │      40.77 │ N          │        455 │          9 │
# => │ 1 │ id2377394 │         1 │ 2016-06-12 00:43:35 │ 2016-06-12 00:54:38 │               1 │           -73.98 │           40.74 │            -74.00 │      40.73 │ N          │        663 │          9 │
# => │ 2 │ id3858529 │         2 │ 2016-01-19 11:35:24 │ 2016-01-19 12:10:48 │               1 │           -73.98 │           40.76 │            -74.01 │      40.71 │ N          │       2124 │          9 │
# => │ 3 │ id3504673 │         2 │ 2016-04-06 19:32:31 │ 2016-04-06 19:39:40 │               1 │           -74.01 │           40.72 │            -74.01 │      40.71 │ N          │        429 │          9 │
# => │ 4 │ id2181028 │         2 │ 2016-03-26 13:30:55 │ 2016-03-26 13:38:10 │               1 │           -73.97 │           40.79 │            -73.97 │      40.78 │ N          │        435 │          9 │
# => ╰───┴───────────┴───────────┴─────────────────────┴─────────────────────┴─────────────────┴──────────────────┴─────────────────┴───────────────────┴────────────┴────────────┴────────────┴────────────╯

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_datetime   │  dropoff_datetime   │ passenger_count │ pickup_longitude │ pickup_latitude │ dropoff_longitude │ dropoff_la │ store_and_ │ trip_durat │ vendor_id_ │
# => │   │           │           │                     │                     │                 │                  │                 │                   │ titude     │ fwd_flag   │ ion        │ lengths    │
# => ├───┼───────────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┼──────────────────┼─────────────────┼───────────────────┼────────────┼────────────┼────────────┼────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14 17:24:55 │ 2016-03-14 17:32:30 │               1 │           -73.98 │           40.77 │            -73.96 │      40.77 │ N          │        455 │          9 │
# => │ 1 │ id2377394 │         1 │ 2016-06-12 00:43:35 │ 2016-06-12 00:54:38 │               1 │           -73.98 │           40.74 │            -74.00 │      40.73 │ N          │        663 │          9 │
# => │ 2 │ id3858529 │         2 │ 2016-01-19 11:35:24 │ 2016-01-19 12:10:48 │               1 │           -73.98 │           40.76 │            -74.01 │      40.71 │ N          │       2124 │          9 │
# => │ 3 │ id3504673 │         2 │ 2016-04-06 19:32:31 │ 2016-04-06 19:39:40 │               1 │           -74.01 │           40.72 │            -74.01 │      40.71 │ N          │        429 │          9 │
# => │ 4 │ id2181028 │         2 │ 2016-03-26 13:30:55 │ 2016-03-26 13:38:10 │               1 │           -73.97 │           40.79 │            -73.97 │      40.78 │ N          │        435 │          9 │
# => ╰───┴───────────┴───────────┴─────────────────────┴─────────────────────┴─────────────────┴──────────────────┴─────────────────┴───────────────────┴────────────┴────────────┴────────────┴────────────╯

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_datetime   │  dropoff_datetime   │ passenger_count │ pickup_longitude │ pickup_latitude │ dropoff_longitude │ dropoff_latitude │ store_and_fwd_fla │ trip_durat │
# => │   │           │           │                     │                     │                 │                  │                 │                   │                  │ g                 │ ion        │
# => ├───┼───────────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┼──────────────────┼─────────────────┼───────────────────┼──────────────────┼───────────────────┼────────────┤
# => │ 0 │ id2875421 │         2 │ 2016-03-14 17:24:55 │ 2016-03-14 17:32:30 │               1 │           -73.98 │           40.77 │            -73.96 │            40.77 │ N                 │       7.58 │
# => │ 1 │ id2377394 │         1 │ 2016-06-12 00:43:35 │ 2016-06-12 00:54:38 │               1 │           -73.98 │           40.74 │            -74.00 │            40.73 │ N                 │      11.05 │
# => │ 2 │ id3858529 │         2 │ 2016-01-19 11:35:24 │ 2016-01-19 12:10:48 │               1 │           -73.98 │           40.76 │            -74.01 │            40.71 │ N                 │      35.40 │
# => │ 3 │ id3504673 │         2 │ 2016-04-06 19:32:31 │ 2016-04-06 19:39:40 │               1 │           -74.01 │           40.72 │            -74.01 │            40.71 │ N                 │       7.15 │
# => │ 4 │ id2181028 │         2 │ 2016-03-26 13:30:55 │ 2016-03-26 13:38:10 │               1 │           -73.97 │           40.79 │            -73.97 │            40.78 │ N                 │       7.25 │
# => ╰───┴───────────┴───────────┴─────────────────────┴─────────────────────┴─────────────────┴──────────────────┴─────────────────┴───────────────────┴──────────────────┴───────────────────┴────────────╯

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

$df | polars filter-with ((polars col trip_duration) >= 500) | polars first 5 | polars collect
# => ╭───┬───────────┬───────────┬─────────────────────┬─────────────────────┬─────────────────┬──────────────────┬─────────────────┬───────────────────┬──────────────────┬───────────────────┬────────────╮
# => │ # │    id     │ vendor_id │   pickup_datetime   │  dropoff_datetime   │ passenger_count │ pickup_longitude │ pickup_latitude │ dropoff_longitude │ dropoff_latitude │ store_and_fwd_fla │ trip_durat │
# => │   │           │           │                     │                     │                 │                  │                 │                   │                  │ g                 │ ion        │
# => ├───┼───────────┼───────────┼─────────────────────┼─────────────────────┼─────────────────┼──────────────────┼─────────────────┼───────────────────┼──────────────────┼───────────────────┼────────────┤
# => │ 0 │ id2377394 │         1 │ 2016-06-12 00:43:35 │ 2016-06-12 00:54:38 │               1 │           -73.98 │           40.74 │            -74.00 │            40.73 │ N                 │        663 │
# => │ 1 │ id3858529 │         2 │ 2016-01-19 11:35:24 │ 2016-01-19 12:10:48 │               1 │           -73.98 │           40.76 │            -74.01 │            40.71 │ N                 │       2124 │
# => │ 2 │ id1324603 │         2 │ 2016-05-21 07:54:58 │ 2016-05-21 08:20:49 │               1 │           -73.97 │           40.80 │            -73.92 │            40.76 │ N                 │       1551 │
# => │ 3 │ id0012891 │         2 │ 2016-03-10 21:45:01 │ 2016-03-10 22:05:26 │               1 │           -73.98 │           40.74 │            -73.97 │            40.79 │ N                 │       1225 │
# => │ 4 │ id1436371 │         2 │ 2016-05-10 22:08:41 │ 2016-05-10 22:29:55 │               1 │           -73.98 │           40.76 │            -74.00 │            40.73 │ N                 │       1274 │
# => ╰───┴───────────┴───────────┴─────────────────────┴─────────────────────┴─────────────────┴──────────────────┴─────────────────┴───────────────────┴──────────────────┴───────────────────┴────────────╯

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