Home Explore Blog CI



nushell

1st chunk of `cookbook/polars_v_pandas_v_nushell.md`
e244d454f50b912d6d450be580fbf731a3fe8dc5e1eb4c2500000001000018ee
---
title: Polars vs Pandas 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

```nu
let df = polars open NYCTaxi.csv
```

```nu
$df | polars shape
# => ╭───┬─────────┬─────────╮
# => │ # │  rows   │ columns │
# => ├───┼─────────┼─────────┤
# => │ 0 │ 1458644 │      11 │
# => ╰───┴─────────┴─────────╯
```

## 2. Opening the file and show the first 5 rows

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

```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       │
# => ├───┼───────────┼───────────┼──────────────┼──────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┼─────────────┤

Title: Polars in Nushell: Opening Files and Basic Operations
Summary
This section demonstrates how to use Polars within Nushell to open a CSV file (NYCTaxi.csv), determine the shape of the resulting DataFrame, display the first 5 rows, and calculate the string lengths of values in the 'id' column. The examples showcase the syntax for performing these operations using Polars commands within the Nushell environment.