Dataframes

TIP

The dataframe commands are available from version 0.33.1 onwards

As we have seen so far, Nushell makes working with data its main priority. Lists and Tables are there to help you cycle through values in order to perform multiple operations or find data in a breeze. However, there are certain operations where a row-based data layout is not the most efficient way to process data, especially when working with extremely large files. Operations like group-by or join using large datasets can be costly memory-wise, and may lead to large computation times if they are not done using the appropriate data format.

For this reason, the DataFrame structure was introduced to Nushell. A DataFrame stores its data in a columnar format using as its base the Apache ArrowDataframes - 图1 (opens new window) specification, and uses PolarsDataframes - 图2 (opens new window) as the motor for performing extremely fast columnar operationsDataframes - 图3 (opens new window).

You may be wondering now how fast this combo could be, and how could it make working with data easier and more reliable. For this reason, let’s start this page by presenting benchmarks on common operations that are done when processing data.

Benchmark comparisons

For this little benchmark exercise we will be comparing native Nushell commands, dataframe Nushell commands and Python PandasDataframes - 图4 (opens new window) commands. For the time being don’t pay too much attention to the dataframe commands. They will be explained in later sections of this page.

System Details: The benchmarks presented in this section were run using a machine with a processor Intel(R) Core(TM) i7-10710U (CPU @1.10GHz 1.61 GHz) and 16 gb of RAM.

All examples were run on Nushell version 0.33.1.

File information

The file that we will be using for the benchmarks is the New Zealand business demographyDataframes - 图5 (opens new window) dataset. Feel free to download it if you want to follow these tests.

The dataset has 5 columns and 5,429,252 rows. We can check that by using the dfr list command:

  1. > let df = (dfr open .\Data7602DescendingYearOrder.csv)
  2. > dfr list
  3. ───┬──────┬─────────┬─────────
  4. # │ name │ rows │ columns
  5. ───┼──────┼─────────┼─────────
  6. 0 $df 5429252 5
  7. ───┴──────┴─────────┴─────────

We can have a look at the first lines of the file using dfr first:

  1. > $df | dfr first
  2. ───┬──────────┬─────────┬──────┬───────────┬──────────
  3. # │ anzsic06 │ Area │ year │ geo_count │ ec_count
  4. ───┼──────────┼─────────┼──────┼───────────┼──────────
  5. 0 A A100100 2000 96 130
  6. 1 A A100200 2000 198 110
  7. 2 A A100300 2000 42 25
  8. 3 A A100400 2000 66 40
  9. 4 A A100500 2000 63 40
  10. ───┴──────────┴─────────┴──────┴───────────┴──────────

…and finally, we can get an idea of the inferred datatypes:

  1. > $df | dfr dtypes
  2. ───┬───────────┬───────
  3. # │ column │ dtype
  4. ───┼───────────┼───────
  5. 0 anzsic06 str
  6. 1 Area str
  7. 2 year i64
  8. 3 geo_count i64
  9. 4 ec_count i64
  10. ───┴───────────┴───────

Loading the file

Let’s start by comparing loading times between the various methods. First, we will load the data using Nushell’s open command:

  1. > benchmark {open .\Data7602DescendingYearOrder.csv}
  2. ───┬─────────────────────────
  3. # │ real time
  4. ───┼─────────────────────────
  5. 0 30sec 479ms 614us 400ns
  6. ───┴─────────────────────────

Loading the file using native Nushell functionality took 30 seconds. Not bad for loading five million records! But we can do a bit better than that.

Let’s now use Pandas. We are going to use the next script to load the file:

  1. import pandas as pd
  2. df = pd.read_csv("Data7602DescendingYearOrder.csv")

And the benchmark for it is:

  1. > benchmark {python load.py}
  2. ───┬───────────────────────
  3. # │ real time
  4. ───┼───────────────────────
  5. 0 2sec 91ms 872us 900ns
  6. ───┴───────────────────────

That is a great improvement, from 30 seconds to 2 seconds. Nicely done, Pandas!

Probably we can load the data a bit faster. This time we will use Nushell’s dfr open command:

  1. > benchmark {dfr open .\Data7602DescendingYearOrder.csv}
  2. ───┬───────────────────
  3. # │ real time
  4. ───┼───────────────────
  5. 0 601ms 700us 700ns
  6. ───┴───────────────────

This time it took us 0.6 seconds. Not bad at all.

Group-by comparison

Let’s do a slightly more complex operation this time. We are going to group the data by year, and add groups using the column geo_count.

Again, we are going to start with a Nushell native command.

TIP

If you want to run this example, be aware that the next command will use a large amount of memory. This may affect the performance of your system while this is being executed.

  1. > benchmark {
  2. open .\Data7602DescendingYearOrder.csv
  3. | group-by year
  4. | pivot header rows
  5. | upsert rows { get rows | math sum }
  6. | flatten
  7. }
  8. ───┬────────────────────────
  9. # │ real time
  10. ───┼────────────────────────
  11. 0 6min 30sec 622ms 312us
  12. ───┴────────────────────────

So, six minutes to perform this aggregated operation.

Let’s try the same operation in pandas:

  1. import pandas as pd
  2. df = pd.read_csv("Data7602DescendingYearOrder.csv")
  3. res = df.groupby("year")["geo_count"].sum()
  4. print(res)

And the result from the benchmark is:

  1. > benchmark {python .\load.py}
  2. ───┬────────────────────────
  3. # │ real time
  4. ───┼────────────────────────
  5. 0 1sec 966ms 954us 800ns
  6. ───┴────────────────────────

Not bad at all. Again, pandas managed to get it done in a fraction of the time.

To finish the comparison, let’s try Nushell dataframes. We are going to put all the operations in one nu file, to make sure we are doing similar operations:

  1. let df = (dfr open Data7602DescendingYearOrder.csv)
  2. let res = ($df | dfr group-by year | dfr aggregate sum | dfr select geo_count)
  3. $res

and the benchmark with dataframes is:

  1. > benchmark {source load.nu}
  2. ───┬───────────────────
  3. # │ real time
  4. ───┼───────────────────
  5. 0 557ms 658us 500ns
  6. ───┴───────────────────

Luckily Nushell dataframes managed to halve the time again. Isn’t that great?

As you can see, Nushell’s Dataframe commands are as fast as the most common tools that exist today to do data analysis. The commands that are included in this release have the potential to become your go-to tool for doing data analysis. By composing complex Nushell pipelines, you can extract information from data in a reliable way.

Working with Dataframes

After seeing a glimpse of the things that can be done with Dataframe commands, now it is time to start testing them. To begin let’s create a sample CSV file that will become our sample dataframe that we will be using along with the examples. In your favorite file editor paste the next lines to create out sample csv file.

  1. int_1,int_2,float_1,float_2,first,second,third,word
  2. 1,11,0.1,1.0,a,b,c,first
  3. 2,12,0.2,1.0,a,b,c,second
  4. 3,13,0.3,2.0,a,b,c,third
  5. 4,14,0.4,3.0,b,a,c,second
  6. 0,15,0.5,4.0,b,a,a,third
  7. 6,16,0.6,5.0,b,a,a,second
  8. 7,17,0.7,6.0,b,c,a,third
  9. 8,18,0.8,7.0,c,c,b,eight
  10. 9,19,0.9,8.0,c,c,b,ninth
  11. 0,10,0.0,9.0,c,c,b,ninth

Save the file and name it however you want to, for the sake of these examples the file will be called test_small.csv.

Now, to read that file as a dataframe use the dfr open command like this:

  1. > let df = (dfr open test_small.csv)

This should create the value df in memory which holds the data we just created.

TIP

The command dfrs open can read either csv or parquet files.

To see all the dataframes that are stored in memory you can use

  1. > dfr list
  2. ───┬──────┬──────┬─────────
  3. # │ name │ rows │ columns
  4. ───┼──────┼──────┼─────────
  5. 0 $df 10 8
  6. ───┴──────┴──────┴─────────

As you can see, the command shows the created dataframes together with basic information about them.

And if you want to see a preview of the loaded dataframe you can send the dataframe variable to the stream

  1. > $df
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────
  5. 0 1 11 0.1000 1.0000 a b c first
  6. 1 2 12 0.2000 1.0000 a b c second
  7. 2 3 13 0.3000 2.0000 a b c third
  8. 3 4 14 0.4000 3.0000 b a c second
  9. 4 0 15 0.5000 4.0000 b a a third
  10. 5 6 16 0.6000 5.0000 b a a second
  11. 6 7 17 0.7000 6.0000 b c a third
  12. 7 8 18 0.8000 7.0000 c c b eight
  13. 8 9 19 0.9000 8.0000 c c b ninth
  14. 9 0 10 0.0000 9.0000 c c b ninth
  15. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────

With the dataframe in memory we can start doing column operations with the DataFrame

TIP

If you want to see all the dataframe commands that are available you can use help dfr

Basic aggregations

Let’s start with basic aggregations on the dataframe. Let’s sum all the columns that exist in df by using the aggregate command

  1. > $df | dfr aggregate sum
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬──────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼──────
  5. 0 40 145 4.5000 46.0000
  6. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴──────

As you can see, the aggregate function computes the sum for those columns where a sum makes sense. If you want to filter out the text column, you can select the columns you want by using the select command

  1. $df | dfr aggregate sum | dfr select int_1 int_2 float_1 float_2
  2. ───┬───────┬───────┬─────────┬─────────
  3. # │ int_1 │ int_2 │ float_1 │ float_2
  4. ───┼───────┼───────┼─────────┼─────────
  5. 0 40 145 4.5000 46.0000
  6. ───┴───────┴───────┴─────────┴─────────

You can even store the result from this aggregation as you would store any other Nushell variable

  1. > let res = ($df | dfr aggregate sum | dfr select int_1 int_2 float_1 float_2)

And now we have two dataframes stored in memory

  1. > dfr list
  2. ───┬──────┬──────┬─────────
  3. # │ name │ rows │ columns
  4. ───┼──────┼──────┼─────────
  5. 0 $df 10 8
  6. 1 $res 1 4
  7. ───┴──────┴──────┴─────────

Pretty neat, isn’t it?

You can perform several aggregations on the dataframe in order to extract basic information from the dataframe and do basic data analysis on your brand new dataframe.

Joining a DataFrame

It is also possible to join two dataframes using a column as reference. We are going to join our mini dataframe with another mini dataframe. Copy these lines in another file and create the corresponding dataframe (for these examples we are going to call it test_small_a.csv)

  1. int_1a,int_2,float_1,float_2,first
  2. 9,14,0.4,3.0,a
  3. 8,13,0.3,2.0,a
  4. 7,12,0.2,1.0,a
  5. 6,11,0.1,0.0,b

We use the dfr open command to create the new variable

  1. > let df_a = (dfr open test_small_a.csv)

Now, with the second dataframe loaded in memory we can join them using the column called int_1 from the left dataframe and the column int_1a from the right dataframe

  1. > $df | dfr join $df_a -l [int_1] -r [int_1a]
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬─────────┬─────────────┬───────────────┬───────────────┬─────────────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word │ int_2_right │ float_1_right │ float_2_right │ first_right
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼─────────┼─────────────┼───────────────┼───────────────┼─────────────
  5. 0 6 16 0.6000 5.0000 b a a second 11 0.1000 0.0000 b
  6. 1 7 17 0.7000 6.0000 b c a third 12 0.2000 1.0000 a
  7. 2 8 18 0.8000 7.0000 c c b eight 13 0.3000 2.0000 a
  8. 3 9 19 0.9000 8.0000 c c b ninth 14 0.4000 3.0000 a
  9. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴─────────┴─────────────┴───────────────┴───────────────┴─────────────

TIP

In Nu when a command has multiple arguments that are expecting multiple values we use brackets [] to enclose those values. In the case of dfr join we can join on multiple columns as long as they have the same type, for example we could have done $df | dfr join $df_a -l [int_1 int_2] -r [int_1a int_2]

By default, the join command does an inner join, meaning that it will keep the rows where both dataframes share the same value. You can select a left join to keep the missing rows from the left dataframe. You can also save this result in order to use it for further operations.

DataFrame group-by

One of the most powerful operations that can be performed with a DataFrame is the group-by. This command will allow you to perform aggregation operations based on a grouping criteria. In Nushell, a GroupBy is a type of object that can be stored and reused for multiple aggregations. This is quite handy, since the creation of the grouped pairs is the most expensive operation while doing group-by and there is no need to repeat it if you are planning to do multiple operations with the same group condition.

To create a GroupBy object you only need to use the group-by command

  1. > let group = ($df | dfr group-by first)
  2. > $group
  3. ───┬──────────┬───────
  4. # │ property │ value
  5. ───┼──────────┼───────
  6. 0 group by first
  7. ───┴──────────┴───────

When printing the GroupBy object we can see the columns that are used as criteria to group the dataframe. Using the GroupBy we can aggregate the dataframe using multiple operations

  1. $group | dfr aggregate sum
  2. ───┬───────┬───────────┬───────────┬─────────────┬─────────────
  3. # │ first │ int_1 │ int_2 │ float_1 │ float_2
  4. ───┼───────┼───────────┼───────────┼─────────────┼─────────────
  5. 0 a 6 36 0.6000 4.0000
  6. 1 b 17 62 2.2000 18.0000
  7. 2 c 17 47 1.7000 24.0000
  8. ───┴───────┴───────────┴───────────┴─────────────┴─────────────

And using the same GroupBy you can perform now another operation on the whole dataframe, like min in this case

  1. $group | aggregate min
  2. ───┬───────┬───────────┬───────────┬─────────────┬─────────────
  3. # │ first │ int_1 │ int_2 │ float_1 │ float_2
  4. ───┼───────┼───────────┼───────────┼─────────────┼─────────────
  5. 0 a 1 11 0.1000 1.0000
  6. 1 b 0 14 0.4000 3.0000
  7. 2 c 0 10 0.0000 7.0000
  8. ───┴───────┴───────────┴───────────┴─────────────┴─────────────

The created GroupBy object is so handy that it can even be used as a base for pivoting a table. As an example, let’s use the column called second as the pivot column and the column float_1 as the value column

  1. > $group | dfr pivot second float_1 sum
  2. ───┬───────┬────────┬────────┬────────
  3. # │ first │ b │ a │ c
  4. ───┼───────┼────────┼────────┼────────
  5. 0 a 0.6000
  6. 1 c 1.7000
  7. 2 b 1.5000 0.7000
  8. ───┴───────┴────────┴────────┴────────

TIP

a pivot operation is a way to aggregate data based on two columns. In the previous example, the result of the pivot command produced a table that represents the sum of all the values in the column float_1 that are shared between columns first (now the rows) and second (now the columns). So, the value of 1.5 shown in row b and column a is the sum of all the floats where the column first is b and column second is a

As you can see, the GroupBy object is a very powerful variable and it is worth keeping in memory while you explore your dataset.

Creating Dataframes

It is also possible to construct dataframes from basic Nushell primitives, such as integers, decimals, or strings. Let’s create a small dataframe using the command to-df.

  1. > let a = ([[a b]; [1 2] [3 4] [5 6]] | dfr to-df)
  2. > $a
  3. ───┬───┬───
  4. # │ b │ a
  5. ───┼───┼───
  6. 0 2 1
  7. 1 4 3
  8. 2 6 5
  9. ───┴───┴───

TIP

For the time being, not all of Nushell primitives can be converted into a dataframe. This will change in the future, as the dataframe feature matures

We can append columns to a dataframe in order to create a new variable. As an example, let’s append two columns to our mini dataframe $a

  1. > let a2 = ($a | dfr with-column $a.a --name a2 | dfr with-column $a.a --name a3)
  2. ───┬───┬───┬────┬────
  3. # │ b │ a │ a2 │ a3
  4. ───┼───┼───┼────┼────
  5. 0 2 1 1 1
  6. 1 4 3 3 3
  7. 2 6 5 5 5
  8. ───┴───┴───┴────┴────

Nushell’s powerful piping syntax allows us to create new dataframes by taking data from other dataframes and appending it to them. Now, if you list your dataframes you will see in total four dataframes

  1. > dfr list
  2. ───┬───────┬──────┬─────────
  3. # │ name │ rows │ columns
  4. ───┼───────┼──────┼─────────
  5. 0 $a 3 2
  6. 1 $a2 3 4
  7. 2 $df_a 4 5
  8. 3 $df 10 8
  9. ───┴───────┴──────┴─────────

One thing that is important to mention is how the memory is being optimized while working with dataframes, and this is thanks to Apache Arrow and Polars. In a very simple representation, each column in a DataFrame is an Arrow Array, which is using several memory specifications in order to maintain the data as packed as possible (check Arrow columnar formatDataframes - 图6 (opens new window)). The other optimization trick is the fact that whenever possible, the columns from the dataframes are shared between dataframes, avoiding memory duplication for the same data. This means that dataframes $a and $a2 are sharing the same two columns we created using the to-df command. For this reason, it isn’t possible to change the value of a column in a dataframe. However, you can create new columns based on data from other columns or dataframes.

Working with Series

A Series is the building block of a DataFrame. Each Series represents a column with the same data type, and we can create multiple Series of different types, such as float, int or string.

Let’s start our exploration with Series by creating one using the to-df command:

  1. > let new = ([9 8 4] | dfr to-df)
  2. > $new
  3. ───┬───
  4. # │ 0
  5. ───┼───
  6. 0 9
  7. 1 8
  8. 2 4
  9. ───┴───

We have created a new series from a list of integers (we could have done the same using floats or strings)

Series have their own basic operations defined, and they can be used to create other Series. Let’s create a new Series by doing some arithmetic on the previously created column.

  1. > let new_2 = ($new * 3 + 10)
  2. > $new_2
  3. ───┬────
  4. # │ 0
  5. ───┼────
  6. 0 37
  7. 1 34
  8. 2 22
  9. ───┴────

Now we have a new Series that was constructed by doing basic operations on the previous variable.

TIP

If you want to see how many variables you have stored in memory you can use $nu.scope.vars

Let’s rename our previous Series so it has a memorable name

  1. > let new_2 = ($new_2 | dfr rename memorable)
  2. > $new_2
  3. ───┬───────────
  4. # │ memorable
  5. ───┼───────────
  6. 0 37
  7. 1 34
  8. 2 22
  9. ───┴───────────

We can also do basic operations with two Series as long as they have the same data type

  1. > $new - $new_2
  2. ───┬──────────
  3. # │ sub_0_0
  4. ───┼──────────
  5. 0 -28
  6. 1 -26
  7. 2 -18
  8. ───┴──────────

And we can add them to previously defined dataframes

  1. > let new_df = ($a | dfr with-column $new --name new_col)
  2. > $new_df
  3. ───┬───┬───┬─────────
  4. # │ b │ a │ new_col
  5. ───┼───┼───┼─────────
  6. 0 2 1 9
  7. 1 4 3 8
  8. 2 6 5 4
  9. ───┴───┴───┴─────────

The Series stored in a Dataframe can also be used directly, for example, we can multiply columns a and b to create a new Series

  1. > $new_df.a * $new_df.b
  2. ───┬─────────
  3. # │ mul_a_b
  4. ───┼─────────
  5. 0 2
  6. 1 12
  7. 2 30
  8. ───┴─────────

and we can start piping things in order to create new columns and dataframes

  1. > let $new_df = ($new_df | dfr with-column ($new_df.a * $new_df.b / $new_df.new_col) --name my_sum)
  2. > let $new_df
  3. ───┬───┬───┬─────────┬────────
  4. # │ b │ a │ new_col │ my_sum
  5. ───┼───┼───┼─────────┼────────
  6. 0 2 1 9 0
  7. 1 4 3 8 1
  8. 2 6 5 4 7
  9. ───┴───┴───┴─────────┴────────

Nushell’s piping system can help you create very interesting workflows.

Series and masks

Series have another key use in when working with DataFrames, and it is the fact that we can build boolean masks out of them. Let’s start by creating a simple mask using the equality operator

  1. > let mask = ($new == 8)
  2. > $mask
  3. ───┬─────────
  4. # │ new_col
  5. ───┼─────────
  6. 0 false
  7. 1 true
  8. 2 false
  9. ───┴─────────

and with this mask we can now filter a dataframe, like this

  1. > $new_df | dfr filter-with $mask
  2. ───┬───┬───┬─────────┬────────
  3. # │ a │ b │ new_col │ my_sum
  4. ───┼───┼───┼─────────┼────────
  5. 0 3 4 8 1
  6. ───┴───┴───┴─────────┴────────

Now we have a new dataframe with only the values where the mask was true.

The masks can also be created from Nushell lists, for example:

  1. > let mask1 = ([true true false] | dfr to-df mask)
  2. > $new_df | dfr filter-with $mask1
  3. ───┬───┬───┬─────────┬────────
  4. # │ a │ b │ new_col │ my_sum
  5. ───┼───┼───┼─────────┼────────
  6. 0 1 2 9 0
  7. 1 3 4 8 1
  8. ───┴───┴───┴─────────┴────────

To create complex masks, we have the AND

  1. > $mask && $mask1
  2. ───┬──────────────────
  3. # │ and_new_col_mask
  4. ───┼──────────────────
  5. 0 false
  6. 1 true
  7. 2 false
  8. ───┴──────────────────

and OR operations

  1. > $mask || $mask1
  2. ───┬─────────────────
  3. # │ or_new_col_mask
  4. ───┼─────────────────
  5. 0 true
  6. 1 true
  7. 2 false
  8. ───┴─────────────────

We can also create a mask by checking if some values exist in other Series. Using the first dataframe that we created we can do something like this

  1. > let mask3 = ($df.first | dfr is-in ([b c] | dfr to-df))
  2. ───┬──────
  3. # │ first
  4. ───┼───────
  5. 0 false
  6. 1 false
  7. 2 false
  8. 3 true
  9. 4 true
  10. 5 true
  11. 6 true
  12. 7 true
  13. 8 true
  14. 9 true
  15. ───┴───────

and this new mask can be used to filter the dataframe

  1. > $df | dfr filter-with $mask3
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬─────────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼─────────
  5. 0 4 14 0.4000 3.0000 b a c second
  6. 1 0 15 0.5000 4.0000 b a a third
  7. 2 6 16 0.6000 5.0000 b a a second
  8. 3 7 17 0.7000 6.0000 b c a third
  9. 4 8 18 0.8000 7.0000 c c b eight
  10. 5 9 19 0.9000 8.0000 c c b ninth
  11. 6 0 10 0.0000 9.0000 c c b ninth
  12. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴─────────

Another operation that can be done with masks is setting or replacing a value from a series. For example, we can change the value in the column first where the value is equal to a

  1. > $df.first | dfr set new --mask ($df.first =~ a)
  2. ───┬────────
  3. # │ string
  4. ───┼────────
  5. 0 new
  6. 1 new
  7. 2 new
  8. 3 b
  9. 4 b
  10. 5 b
  11. 6 b
  12. 7 c
  13. 8 c
  14. 9 c
  15. ───┴────────

Series as indices

Series can be also used as a way of filtering a dataframe by using them as a list of indices. For example, let’s say that we want to get rows 1, 4, and 6 from our original dataframe. With that in mind, we can use the next command to extract that information

  1. > let indices = ([1 4 6] | dfr to-df)
  2. > $df | dfr take $indices
  3. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────
  4. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  5. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────
  6. 0 2 12 0.2000 1.0000 a b c second
  7. 1 0 15 0.5000 4.0000 b a a third
  8. 2 7 17 0.7000 6.0000 b c a third
  9. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────

The command take is very handy, especially if we mix it with other commands. Let’s say that we want to extract all rows for the first duplicated element for column first. In order to do that, we can use the command dfr arg-unique as shown in the next example

  1. > let indices = ($df.first | dfr arg-unique)
  2. > $df | dfr take $indices
  3. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────
  4. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  5. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────
  6. 0 1 11 0.1000 1.0000 a b c first
  7. 1 4 14 0.4000 3.0000 b a c second
  8. 2 8 18 0.8000 7.0000 c c b eight
  9. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────

Or what if we want to create a new sorted dataframe using a column in specific. We can use the dfr arg-sort to accomplish that. In the next example we can sort the dataframe by the column word

TIP

The same result could be accomplished using the command sort

  1. > let indices = ($df.word | dfr arg-sort)
  2. > $df | dfr take $indices
  3. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────
  4. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  5. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────
  6. 0 8 18 0.8000 7.0000 c c b eight
  7. 1 1 11 0.1000 1.0000 a b c first
  8. 2 9 19 0.9000 8.0000 c c b ninth
  9. 3 0 10 0.0000 9.0000 c c b ninth
  10. 4 2 12 0.2000 1.0000 a b c second
  11. 5 4 14 0.4000 3.0000 b a c second
  12. 6 6 16 0.6000 5.0000 b a a second
  13. 7 3 13 0.3000 2.0000 a b c third
  14. 8 0 15 0.5000 4.0000 b a a third
  15. 9 7 17 0.7000 6.0000 b c a third
  16. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────

And finally, we can create new Series by setting a new value in the marked indices. Have a look at the next command

  1. > let indices = ([0 2] | dfr to-df);
  2. > $df.int_1 | dfr set-with-idx 123 --indices $indices
  3. ───┬───────
  4. # │ int_1
  5. ───┼───────
  6. 0 123
  7. 1 2
  8. 2 123
  9. 3 4
  10. 4 0
  11. 5 6
  12. 6 7
  13. 7 8
  14. 8 9
  15. 9 0
  16. ───┴───────

Unique values

Another operation that can be done with Series is to search for unique values in a list or column. Lets use again the first dataframe we created to test these operations.

The first and most common operation that we have is value_counts. This command calculates a count of the unique values that exist in a Series. For example, we can use it to count how many occurrences we have in the column first

  1. > $df.first | dfr value-counts
  2. ───┬───────┬────────
  3. # │ first │ counts
  4. ───┼───────┼────────
  5. 0 b 4
  6. 1 c 3
  7. 2 a 3
  8. ───┴───────┴────────

As expected, the command returns a new dataframe that can be used to do more queries.

Continuing with our exploration of Series, the next thing that we can do is to only get the unique unique values from a series, like this

  1. > $df.first | dfr unique
  2. ───┬───────
  3. # │ first
  4. ───┼───────
  5. 0 c
  6. 1 b
  7. 2 a
  8. ───┴───────

Or we can get a mask that we can use to filter out the rows where data is unique or duplicated. For example, we can select the rows for unique values in column word

  1. > $df | dfr filter-with ($df.word | dfr is-unique)
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬───────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼───────
  5. 0 1 11 0.1000 1.0000 a b c first
  6. 1 8 18 0.8000 7.0000 c c b eight
  7. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴───────

Or all the duplicated ones

  1. > $df | dfr filter-with ($df.word | dfr is-duplicated)
  2. ───┬───────┬───────┬─────────┬─────────┬───────┬────────┬───────┬────────
  3. # │ int_1 │ int_2 │ float_1 │ float_2 │ first │ second │ third │ word
  4. ───┼───────┼───────┼─────────┼─────────┼───────┼────────┼───────┼────────
  5. 0 2 12 0.2000 1.0000 a b c second
  6. 1 3 13 0.3000 2.0000 a b c third
  7. 2 4 14 0.4000 3.0000 b a c second
  8. 3 0 15 0.5000 4.0000 b a a third
  9. 4 6 16 0.6000 5.0000 b a a second
  10. 5 7 17 0.7000 6.0000 b c a third
  11. 6 9 19 0.9000 8.0000 c c b ninth
  12. 7 0 10 0.0000 9.0000 c c b ninth
  13. ───┴───────┴───────┴─────────┴─────────┴───────┴────────┴───────┴────────

Dataframe commands

So far we have seen quite a few operations that can be done using DataFrames commands. However, the commands we have used so far are not all the commands available to work with data and be assured that there will be more as the feature becomes more stable.

The next list shows the available dataframe commands with their descriptions, and whenever possible, their analogous Nushell command.

Command NameApplies ToDescriptionNushell Equivalent
aggregateDataFrame, GroupBy, SeriesPerforms an aggregation operation on a dataframe, groupby or series objectmath
all-falseSeriesReturns true if all values are false
all-trueSeriesReturns true if all values are trueall?
arg-maxSeriesReturn index for max value in series
arg-minSeriesReturn index for min value in series
arg-sortSeriesReturns indexes for a sorted series
arg-trueSeriesReturns indexes where values are true
arg-uniqueSeriesReturns indexes for unique values
columnDataFrameReturns the selected column as Seriesget
count-nullSeriesCounts null values
count-uniqueSeriesCounts unique value
dropDataFrameCreates a new dataframe by dropping the selected columnsdrop
drop-duplicatesDataFrameDrops duplicate values in dataframe
drop-nullsDataFrame, SeriesDrops null values in dataframe
dtypesDataFrameShow dataframe data types
filter-withDataFrameFilters dataframe using a mask as reference
firstDataFrameCreates new dataframe with first rowsfirst
getDataFrameCreates dataframe with the selected columnsget
group-byDataFrameCreates a groupby object that can be used for other aggregationsgroup-by
is-duplicatedSeriesCreates mask indicating duplicated values
is-inSeriesChecks if elements from a series are contained in right seriesin
is-not-nullSeriesCreates mask where value is not null
is-nullSeriesCreates mask where value is null<column_name> == $nothing
is-uniqueSeriesCreates mask indicating unique values
joinDataFrameJoins a dataframe using columns as reference
lastDataFrameCreates new dataframe with last rowslast
listLists stored dataframes
meltDataFrameUnpivot a DataFrame from wide to long format
notSeries Inverts boolean mask
openLoads dataframe form csv fileopen
pivotGroupByPerforms a pivot operation on a groupby objectpivot
renameSeriesRenames a seriesrename
sampleDataFrameCreate sample dataframe
selectDataFrameCreates a new dataframe with the selected columnsselect
setSeriesSets value where given mask is true
set-with-idxSeriesSets value in the given index
shiftSeriesShifts the values by a given period
showDataFrameConverts a section of the dataframe to a Table or List value
sliceDataFrameCreates new dataframe from a slice of rows
sortDataFrame, SeriesCreates new sorted dataframe or seriessort
takeDataFrame, SeriesCreates new dataframe using the given indices
to-csvDataFrameSaves dataframe to csv fileto csv
to-dfConverts a pipelined Table or List into Dataframe
to-dummiesDataFrameCreates a new dataframe with dummy variables
to-parquetDataFrameSaves dataframe to parquet file
uniqueSeriesReturns unique values from a seriesuniq
value-countsSeriesReturns a dataframe with the counts for unique values in series
whereDataFrameFilter dataframe to match the conditionwhere
with-columnDataFrameAdds a series to the dataframeinsert <column_name> <value> | upsert <column_name> { <new_value> }

Future of Dataframes

We hope that by the end of this page you have a solid grasp of how to use the dataframe commands. As you can see they offer powerful operations that can help you process data faster and natively.

However, the future of these dataframes is still very experimental. New commands and tools that take advantage of these commands will be added as they mature. For example, the next step for dataframes is the introduction of Lazy Dataframes. These will allow you to define complex data operations that will be executed until you decide to “finish” the pipe. This will give Nushell the chance to select the optimal plan to query the data you would be asking for.

Keep visiting this book in order to check the new things happening to dataframes and how they can help you process data faster and efficiently.