File size: 58,542 Bytes
8d756d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 |
# Dataframes
::: warning
To use the dataframe support you need a fully-featured build with `cargo build --features dataframe`. Starting with version 0.72, dataframes are *not* included with binary releases of Nushell. [See the installation instructions](/book/installation.md) for further details.
:::
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
Arrow](https://arrow.apache.org/) specification, and uses
[Polars](https://github.com/pola-rs/polars) as the motor for performing
extremely [fast columnar operations](https://h2oai.github.io/db-benchmark/).
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
Pandas](https://pandas.pydata.org/) commands. For the time being don't pay too
much attention to the [`Dataframe` commands](/commands/categories/dataframe.md). 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.
> (Command names are updated to Nushell 0.78)
### File information
The file that we will be using for the benchmarks is the
[New Zealand business demography](https://www.stats.govt.nz/assets/Uploads/New-Zealand-business-demography-statistics/New-Zealand-business-demography-statistics-At-February-2020/Download-data/Geographic-units-by-industry-and-statistical-area-2000-2020-descending-order-CSV.zip) 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 ls` command:
```nu
โฏ let df = (dfr open .\Data7602DescendingYearOrder.csv)
โฏ dfr ls
โญโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ # โ name โ columns โ rows โ
โโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ 0 โ $df โ 5 โ 5429252 โ
โฐโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
```
We can have a look at the first lines of the file using [`first`](/commands/docs/first.md):
```nu
โฏ $df | dfr first
โญโโโโฌโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโฎ
โ # โ anzsic06 โ Area โ year โ geo_count โ ec_count โ
โโโโโผโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโค
โ 0 โ A โ A100100 โ 2000 โ 96 โ 130 โ
โฐโโโโดโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโฏ
```
...and finally, we can get an idea of the inferred data types:
```nu
โฏ $df | dfr dtypes
โญโโโโฌโโโโโโโโโโโโฌโโโโโโโโฎ
โ # โ column โ dtype โ
โโโโโผโโโโโโโโโโโโผโโโโโโโโค
โ 0 โ anzsic06 โ str โ
โ 1 โ Area โ str โ
โ 2 โ year โ i64 โ
โ 3 โ geo_count โ i64 โ
โ 4 โ ec_count โ i64 โ
โฐโโโโดโโโโโโโโโโโโดโโโโโโโโฏ
```
### Loading the file
Let's start by comparing loading times between the various methods. First, we
will load the data using Nushell's [`open`](/commands/docs/open.md) command:
```nu
โฏ timeit {open .\Data7602DescendingYearOrder.csv}
30sec 479ms 614us 400ns
```
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:
```python
import pandas as pd
df = pd.read_csv("Data7602DescendingYearOrder.csv")
```
And the benchmark for it is:
```nu
โฏ timeit {python load.py}
2sec 91ms 872us 900ns
```
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:
```nu
โฏ timeit {dfr open .\Data7602DescendingYearOrder.csv}
601ms 700us 700ns
```
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.
:::
```nu
โฏ timeit {
open .\Data7602DescendingYearOrder.csv
| group-by year
| transpose header rows
| upsert rows { get rows | math sum }
| flatten
}
6min 30sec 622ms 312us
```
So, six minutes to perform this aggregated operation.
Let's try the same operation in pandas:
```python
import pandas as pd
df = pd.read_csv("Data7602DescendingYearOrder.csv")
res = df.groupby("year")["geo_count"].sum()
print(res)
```
And the result from the benchmark is:
```nu
โฏ timeit {python .\load.py}
1sec 966ms 954us 800ns
```
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:
```nu
let df = (dfr open Data7602DescendingYearOrder.csv)
let res = ($df | dfr group-by year | dfr agg (dfr col geo_count | dfr sum))
$res
```
and the benchmark with dataframes is:
```nu
โฏ timeit {source load.nu}
557ms 658us 500ns
```
Luckily Nushell dataframes managed to halve the time again. Isn't that great?
As you can see, Nushell's [`Dataframe` commands](/commands/categories/dataframe.md)
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](/commands/categories/dataframe.md),
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.
```
int_1,int_2,float_1,float_2,first,second,third,word
1,11,0.1,1.0,a,b,c,first
2,12,0.2,1.0,a,b,c,second
3,13,0.3,2.0,a,b,c,third
4,14,0.4,3.0,b,a,c,second
0,15,0.5,4.0,b,a,a,third
6,16,0.6,5.0,b,a,a,second
7,17,0.7,6.0,b,c,a,third
8,18,0.8,7.0,c,c,b,eight
9,19,0.9,8.0,c,c,b,ninth
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:
```nu
โฏ 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 `dfr open` can read either **csv** or **parquet**
files.
:::
To see all the dataframes that are stored in memory you can use
```nu
โฏ dfr ls
โญโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโฎ
โ # โ name โ columns โ rows โ
โโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 0 โ $df โ 8 โ 10 โ
โฐโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโฏ
```
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
```nu
โฏ $df
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 1 โ 11 โ 0.10 โ 1.00 โ a โ b โ c โ first โ
โ 1 โ 2 โ 12 โ 0.20 โ 1.00 โ a โ b โ c โ second โ
โ 2 โ 3 โ 13 โ 0.30 โ 2.00 โ a โ b โ c โ third โ
โ 3 โ 4 โ 14 โ 0.40 โ 3.00 โ b โ a โ c โ second โ
โ 4 โ 0 โ 15 โ 0.50 โ 4.00 โ b โ a โ a โ third โ
โ 5 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ
โ 6 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ
โ 7 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ
โ 8 โ 9 โ 19 โ 0.90 โ 8.00 โ c โ c โ b โ ninth โ
โ 9 โ 0 โ 10 โ 0.00 โ 9.00 โ c โ c โ b โ ninth โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
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 `scope commands | where category =~ dataframe`
:::
## 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
```nu
โฏ $df | dfr sum
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโค
โ 0 โ 40 โ 145 โ 4.50 โ 46.00 โ โ โ โ โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโฏ
```
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 [`dfr select`](/commands/docs/dfr_select.md) command
```nu
โฏ $df | dfr sum | dfr select int_1 int_2 float_1 float_2
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ 0 โ 40 โ 145 โ 4.50 โ 46.00 โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
```
You can even store the result from this aggregation as you would store any
other Nushell variable
```nu
โฏ let res = ($df | dfr sum | dfr select int_1 int_2 float_1 float_2)
```
::: tip
Type `let res = ( !! )` and press enter. This will auto complete the previously
executed command. Note the space between ( and !!.
:::
And now we have two dataframes stored in memory
```nu
โฏ dfr ls
โญโโโโฌโโโโโโโฌโโโโโโโโโโฌโโโโโโโฎ
โ # โ name โ columns โ rows โ
โโโโโผโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 0 โ $res โ 4 โ 1 โ
โ 1 โ $df โ 8 โ 10 โ
โฐโโโโดโโโโโโโดโโโโโโโโโโดโโโโโโโฏ
```
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`)
```
int_1,int_2,float_1,float_2,first
9,14,0.4,3.0,a
8,13,0.3,2.0,a
7,12,0.2,1.0,a
6,11,0.1,0.0,b
```
We use the `dfr open` command to create the new variable
```nu
โฏ 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_1` from the
right dataframe
```nu
โฏ $df | dfr join $df_a int_1 int_1
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ int_2_x โ float_1_x โ float_2_x โ first_x โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโค
โ 0 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ 11 โ 0.10 โ 0.00 โ b โ
โ 1 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ 12 โ 0.20 โ 1.00 โ a โ
โ 2 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ 13 โ 0.30 โ 2.00 โ a โ
โ 3 โ 9 โ 19 โ 0.90 โ 8.00 โ c โ c โ b โ ninth โ 14 โ 0.40 โ 3.00 โ a โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโฏ
```
::: 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`](/commands/docs/dfr_join.md) we can join on multiple columns
as long as they have the same type.
:::
For example:
```nu
โฏ $df | dfr join $df_a [int_1 first] [int_1 first]
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ int_2_x โ float_1_x โ float_2_x โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโโผโโโโโโโโโโโโค
โ 0 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ 11 โ 0.10 โ 0.00 โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโฏ
```
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 [`dfr group-by`](/commands/docs/dfr_group-by.md). 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 [`dfr_group-by`](/commands/docs/dfr_group-by.md) command
```nu
โฏ let group = ($df | dfr group-by first)
โฏ $group
โญโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ LazyGroupBy โ apply aggregation to complete execution plan โ
โฐโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
```
When printing the `GroupBy` object we can see that it is in the background a
lazy operation waiting to be completed by adding an aggregation. Using the
`GroupBy` we can create aggregations on a column
```nu
โฏ $group | dfr agg (dfr col int_1 | dfr sum)
โญโโโโฌโโโโโโโโฌโโโโโโโโฎ
โ # โ first โ int_1 โ
โโโโโผโโโโโโโโผโโโโโโโโค
โ 0 โ b โ 17 โ
โ 1 โ a โ 6 โ
โ 2 โ c โ 17 โ
โฐโโโโดโโโโโโโโดโโโโโโโโฏ
```
or we can define multiple aggregations on the same or different columns
```nu
โฏ $group | dfr agg [
โ (dfr col int_1 | dfr n-unique)
โ (dfr col int_2 | dfr min)
โ (dfr col float_1 | dfr sum)
โ (dfr col float_2 | dfr count)
โ ] | dfr sort-by first
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฎ
โ # โ first โ int_1 โ int_2 โ float_1 โ float_2 โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ 0 โ a โ 3 โ 11 โ 0.60 โ 3 โ
โ 1 โ b โ 4 โ 14 โ 2.20 โ 4 โ
โ 2 โ c โ 3 โ 10 โ 1.70 โ 3 โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโฏ
```
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 `dfr into-df`.
```nu
โฏ let a = ([[a b]; [1 2] [3 4] [5 6]] | dfr into-df)
โฏ $a
```
::: 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`
```nu
โฏ let a2 = ($a | dfr with-column $a.a --name a2 | dfr with-column $a.a --name a3)
โฏ $a2
โญโโโโฌโโโโฌโโโโฌโโโโโฌโโโโโฎ
โ # โ a โ b โ a2 โ a3 โ
โโโโโผโโโโผโโโโผโโโโโผโโโโโค
โ 0 โ 1 โ 2 โ 1 โ 1 โ
โ 1 โ 3 โ 4 โ 3 โ 3 โ
โ 2 โ 5 โ 6 โ 5 โ 5 โ
โฐโโโโดโโโโดโโโโดโโโโโดโโโโโฏ
```
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
```nu
โฏ dfr ls
โญโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโฎ
โ # โ name โ columns โ rows โ
โโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโค
โ 0 โ $a2 โ 4 โ 3 โ
โ 1 โ $df_a โ 5 โ 4 โ
โ 2 โ $df โ 8 โ 10 โ
โ 3 โ $a โ 2 โ 3 โ
โ 4 โ $res โ 4 โ 1 โ
โฐโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโฏ
```
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
format](https://arrow.apache.org/docs/format/Columnar.html)). 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 `dfr into-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 `dfr into-df`
command:
```nu
โฏ let new = ([9 8 4] | dfr into-df)
โฏ $new
โญโโโโฌโโโโฎ
โ # โ 0 โ
โโโโโผโโโโค
โ 0 โ 9 โ
โ 1 โ 8 โ
โ 2 โ 4 โ
โฐโโโโดโโโโฏ
```
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.
```nu
โฏ let new_2 = ($new * 3 + 10)
โฏ $new_2
โญโโโโฌโโโโโฎ
โ # โ 0 โ
โโโโโผโโโโโค
โ 0 โ 37 โ
โ 1 โ 34 โ
โ 2 โ 22 โ
โฐโโโโดโโโโโฏ
```
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 `scope variables`
:::
Let's rename our previous Series so it has a memorable name
```nu
โฏ let new_2 = ($new_2 | dfr rename "0" memorable)
โฏ $new_2
โญโโโโฌโโโโโโโโโโโโฎ
โ # โ memorable โ
โโโโโผโโโโโโโโโโโโค
โ 0 โ 37 โ
โ 1 โ 34 โ
โ 2 โ 22 โ
โฐโโโโดโโโโโโโโโโโโฏ
```
We can also do basic operations with two Series as long as they have the same
data type
```nu
โฏ $new - $new_2
โญโโโโฌโโโโโโโโโโโโโโโโโโฎ
โ # โ sub_0_memorable โ
โโโโโผโโโโโโโโโโโโโโโโโโค
โ 0 โ -28 โ
โ 1 โ -26 โ
โ 2 โ -18 โ
โฐโโโโดโโโโโโโโโโโโโโโโโโฏ
```
And we can add them to previously defined dataframes
```nu
โฏ let new_df = ($a | dfr with-column $new --name new_col)
โฏ $new_df
โญโโโโฌโโโโฌโโโโฌโโโโโโโโโโฎ
โ # โ a โ b โ new_col โ
โโโโโผโโโโผโโโโผโโโโโโโโโโค
โ 0 โ 1 โ 2 โ 9 โ
โ 1 โ 3 โ 4 โ 8 โ
โ 2 โ 5 โ 6 โ 4 โ
โฐโโโโดโโโโดโโโโดโโโโโโโโโโฏ
```
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
```nu
โฏ $new_df.a * $new_df.b
โญโโโโฌโโโโโโโโโโฎ
โ # โ mul_a_b โ
โโโโโผโโโโโโโโโโค
โ 0 โ 2 โ
โ 1 โ 12 โ
โ 2 โ 30 โ
โฐโโโโดโโโโโโโโโโฏ
```
and we can start piping things in order to create new columns and dataframes
```nu
โฏ let $new_df = ($new_df | dfr with-column ($new_df.a * $new_df.b / $new_df.new_col) --name my_sum)
โฏ $new_df
โญโโโโฌโโโโฌโโโโฌโโโโโโโโโโฌโโโโโโโโโฎ
โ # โ a โ b โ new_col โ my_sum โ
โโโโโผโโโโผโโโโผโโโโโโโโโโผโโโโโโโโโค
โ 0 โ 1 โ 2 โ 9 โ 0 โ
โ 1 โ 3 โ 4 โ 8 โ 1 โ
โ 2 โ 5 โ 6 โ 4 โ 7 โ
โฐโโโโดโโโโดโโโโดโโโโโโโโโโดโโโโโโโโโฏ
```
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
```nu
โฏ let mask = ($new == 8)
โฏ $mask
โญโโโโฌโโโโโโโโฎ
โ # โ 0 โ
โโโโโผโโโโโโโโค
โ 0 โ false โ
โ 1 โ true โ
โ 2 โ false โ
โฐโโโโดโโโโโโโโฏ
```
and with this mask we can now filter a dataframe, like this
```nu
โฏ $new_df | dfr filter-with $mask
โญโโโโฌโโโโฌโโโโฌโโโโโโโโโโฌโโโโโโโโโฎ
โ # โ a โ b โ new_col โ my_sum โ
โโโโโผโโโโผโโโโผโโโโโโโโโโผโโโโโโโโโค
โ 0 โ 3 โ 4 โ 8 โ 1 โ
โฐโโโโดโโโโดโโโโดโโโโโโโโโโดโโโโโโโโโฏ
```
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:
```nu
โฏ let mask1 = ([true true false] | dfr into-df)
โฏ $new_df | dfr filter-with $mask1
โญโโโโฌโโโโฌโโโโฌโโโโโโโโโโฌโโโโโโโโโฎ
โ # โ a โ b โ new_col โ my_sum โ
โโโโโผโโโโผโโโโผโโโโโโโโโโผโโโโโโโโโค
โ 0 โ 1 โ 2 โ 9 โ 0 โ
โ 1 โ 3 โ 4 โ 8 โ 1 โ
โฐโโโโดโโโโดโโโโดโโโโโโโโโโดโโโโโโโโโฏ
```
To create complex masks, we have the `AND`
```nu
โฏ $mask and $mask1
โญโโโโฌโโโโโโโโโโฎ
โ # โ and_0_0 โ
โโโโโผโโโโโโโโโโค
โ 0 โ false โ
โ 1 โ true โ
โ 2 โ false โ
โฐโโโโดโโโโโโโโโโฏ
```
and `OR` operations
```nu
โฏ $mask or $mask1
โญโโโโฌโโโโโโโโโฎ
โ # โ or_0_0 โ
โโโโโผโโโโโโโโโค
โ 0 โ true โ
โ 1 โ true โ
โ 2 โ false โ
โฐโโโโดโโโโโโโโโฏ
```
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
```nu
โฏ let mask3 = ($df | dfr col first | dfr is-in [b c])
โฏ $mask3
โญโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ โ โญโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโโโโฎ โ
โ input โ โ # โ expr โ value โ โ
โ โ โโโโโผโโโโโโโโโโผโโโโโโโโโโโโโโโค โ
โ โ โ 0 โ column โ first โ โ
โ โ โ 1 โ literal โ Series[list] โ โ
โ โ โฐโโโโดโโโโโโโโโโดโโโโโโโโโโโโโโโฏ โ
โ function โ IsIn โ
โ options โ FunctionOptions { collect_groups: ApplyFlat, input_wildcard_expansion: false, auto_explode: tru โ
โ โ e, fmt_str: "", cast_to_supertypes: true, allow_rename: false, pass_name_to_apply: false } โ
โฐโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
```
and this new mask can be used to filter the dataframe
```nu
โฏ $df | dfr filter-with $mask3
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 4 โ 14 โ 0.40 โ 3.00 โ b โ a โ c โ second โ
โ 1 โ 0 โ 15 โ 0.50 โ 4.00 โ b โ a โ a โ third โ
โ 2 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ
โ 3 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ
โ 4 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ
โ 5 โ 9 โ 19 โ 0.90 โ 8.00 โ c โ c โ b โ ninth โ
โ 6 โ 0 โ 10 โ 0.00 โ 9.00 โ c โ c โ b โ ninth โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
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`
::: warning
This is example is not updated to recent Nushell versions.
:::
```nu
โฏ $df | dfr get first | dfr set new --mask ($df.first =~ a)
โญโโโโฌโโโโโโโโโฎ
โ # โ string โ
โโโโโผโโโโโโโโโค
โ 0 โ new โ
โ 1 โ new โ
โ 2 โ new โ
โ 3 โ b โ
โ 4 โ b โ
โ 5 โ b โ
โ 6 โ b โ
โ 7 โ c โ
โ 8 โ c โ
โ 9 โ c โ
โฐโโโโดโโโโโโโโโฏ
```
## 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
```nu
โฏ let indices = ([1 4 6] | dfr into-df)
โฏ $df | dfr take $indices
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 2 โ 12 โ 0.20 โ 1.00 โ a โ b โ c โ second โ
โ 1 โ 0 โ 15 โ 0.50 โ 4.00 โ b โ a โ a โ third โ
โ 2 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
The command [`dfr take`](/commands/docs/dfr_take.md) 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
```nu
โฏ let indices = ($df | dfr get first | dfr arg-unique)
โฏ $df | dfr take $indices
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 1 โ 11 โ 0.10 โ 1.00 โ a โ b โ c โ first โ
โ 1 โ 4 โ 14 โ 0.40 โ 3.00 โ b โ a โ c โ second โ
โ 2 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
Or what if we want to create a new sorted dataframe using a column in specific.
We can use the `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`](/commands/docs/sort.md)
:::
```nu
โฏ let indices = ($df | dfr get word | dfr arg-sort)
โฏ $df | dfr take $indices
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ
โ 1 โ 1 โ 11 โ 0.10 โ 1.00 โ a โ b โ c โ first โ
โ 2 โ 9 โ 19 โ 0.90 โ 8.00 โ c โ c โ b โ ninth โ
โ 3 โ 0 โ 10 โ 0.00 โ 9.00 โ c โ c โ b โ ninth โ
โ 4 โ 2 โ 12 โ 0.20 โ 1.00 โ a โ b โ c โ second โ
โ 5 โ 4 โ 14 โ 0.40 โ 3.00 โ b โ a โ c โ second โ
โ 6 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ
โ 7 โ 3 โ 13 โ 0.30 โ 2.00 โ a โ b โ c โ third โ
โ 8 โ 0 โ 15 โ 0.50 โ 4.00 โ b โ a โ a โ third โ
โ 9 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
And finally, we can create new Series by setting a new value in the marked
indices. Have a look at the next command
```nu
โฏ let indices = ([0 2] | dfr into-df);
โฏ $df | dfr get int_1 | dfr set-with-idx 123 --indices $indices
โญโโโโฌโโโโโโโโฎ
โ # โ int_1 โ
โโโโโผโโโโโโโโค
โ 0 โ 123 โ
โ 1 โ 2 โ
โ 2 โ 123 โ
โ 3 โ 4 โ
โ 4 โ 0 โ
โ 5 โ 6 โ
โ 6 โ 7 โ
โ 7 โ 8 โ
โ 8 โ 9 โ
โ 9 โ 0 โ
โฐโโโโดโโโโโโโโฏ
```
## 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`
```nu
โฏ $df | dfr get first | dfr value-counts
โญโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ first โ counts โ
โโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ b โ 4 โ
โ 1 โ a โ 3 โ
โ 2 โ c โ 3 โ
โฐโโโโดโโโโโโโโดโโโโโโโโโฏ
```
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
```nu
โฏ $df | dfr get first | dfr unique
โญโโโโฌโโโโโโโโฎ
โ # โ first โ
โโโโโผโโโโโโโโค
โ 0 โ c โ
โ 1 โ b โ
โ 2 โ a โ
โฐโโโโดโโโโโโโโฏ
```
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`
```nu
โฏ $df | dfr filter-with ($df | dfr get word | dfr is-unique)
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโค
โ 0 โ 1 โ 11 โ 0.10 โ 1.00 โ a โ b โ c โ first โ
โ 1 โ 8 โ 18 โ 0.80 โ 7.00 โ c โ c โ b โ eight โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโฏ
```
Or all the duplicated ones
```nu
โฏ $df | dfr filter-with ($df | dfr get word | dfr is-duplicated)
โญโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฎ
โ # โ int_1 โ int_2 โ float_1 โ float_2 โ first โ second โ third โ word โ
โโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโผโโโโโโโโโผโโโโโโโโผโโโโโโโโโค
โ 0 โ 2 โ 12 โ 0.20 โ 1.00 โ a โ b โ c โ second โ
โ 1 โ 3 โ 13 โ 0.30 โ 2.00 โ a โ b โ c โ third โ
โ 2 โ 4 โ 14 โ 0.40 โ 3.00 โ b โ a โ c โ second โ
โ 3 โ 0 โ 15 โ 0.50 โ 4.00 โ b โ a โ a โ third โ
โ 4 โ 6 โ 16 โ 0.60 โ 5.00 โ b โ a โ a โ second โ
โ 5 โ 7 โ 17 โ 0.70 โ 6.00 โ b โ c โ a โ third โ
โ 6 โ 9 โ 19 โ 0.90 โ 8.00 โ c โ c โ b โ ninth โ
โ 7 โ 0 โ 10 โ 0.00 โ 9.00 โ c โ c โ b โ ninth โ
โฐโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโโโดโโโโโโโโโฏ
```
## Lazy Dataframes
Lazy dataframes are a way to query data by creating a logical plan. The
advantage of this approach is that the plan never gets evaluated until you need
to extract data. This way you could chain together aggregations, joins and
selections and collect the data once you are happy with the selected
operations.
Let's create a small example of a lazy dataframe
```nu
โฏ let a = ([[a b]; [1 a] [2 b] [3 c] [4 d]] | dfr into-lazy)
โฏ $a
โญโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ plan โ DF ["a", "b"]; PROJECT */2 COLUMNS; SELECTION: "None" โ
โ โ โ
โ optimized_plan โ DF ["a", "b"]; PROJECT */2 COLUMNS; SELECTION: "None" โ
โ โ โ
โฐโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
```
As you can see, the resulting dataframe is not yet evaluated, it stays as a
set of instructions that can be done on the data. If you were to collect that
dataframe you would get the next result
```nu
โฏ $a | dfr collect
โญโโโโฌโโโโฌโโโโฎ
โ # โ a โ b โ
โโโโโผโโโโผโโโโค
โ 0 โ 1 โ a โ
โ 1 โ 2 โ b โ
โ 2 โ 3 โ c โ
โ 3 โ 4 โ d โ
โฐโโโโดโโโโดโโโโฏ
```
as you can see, the collect command executes the plan and creates a nushell
table for you.
All dataframes operations should work with eager or lazy dataframes. They are
converted in the background for compatibility. However, to take advantage of
lazy operations if is recommended to only use lazy operations with lazy
dataframes.
To find all lazy dataframe operations you can use
```nu
$nu.scope.commands | where category =~ lazyframe
```
With your lazy frame defined we can start chaining operations on it. For
example this
```nu
โฏ $a |
โ dfr reverse |
โ dfr with-column [
โ ((dfr col a) * 2 | dfr as double_a)
โ ((dfr col a) / 2 | dfr as half_a)
โ ] | dfr collect
โญโโโโฌโโโโฌโโโโฌโโโโโโโโโโโฌโโโโโโโโโฎ
โ # โ a โ b โ double_a โ half_a โ
โโโโโผโโโโผโโโโผโโโโโโโโโโโผโโโโโโโโโค
โ 0 โ 4 โ d โ 8 โ 2 โ
โ 1 โ 3 โ c โ 6 โ 1 โ
โ 2 โ 2 โ b โ 4 โ 1 โ
โ 3 โ 1 โ a โ 2 โ 0 โ
โฐโโโโดโโโโดโโโโดโโโโโโโโโโโดโโโโโโโโโฏ
```
:::tip
You can use the line buffer editor to format your queries (`ctr + o`) easily
:::
This query uses the lazy reverse command to invert the dataframe and the
`dfr with-column` command to create new two columns using `expressions`.
An `expression` is used to define an operation that is executed on the lazy
frame. When put together they create the whole set of instructions used by the
lazy commands to query the data. To list all the commands that generate an
expression you can use
```nu
scope commands | where category =~ expression
```
In our previous example, we use the `dfr col` command to indicate that column `a`
will be multiplied by 2 and then it will be aliased to the name `double_a`.
In some cases the use of the `dfr col` command can be inferred. For example,
using the `dfr select` command we can use only a string
```nu
> $a | dfr select a | dfr collect
```
or the `dfr col` command
```nu
> $a | dfr select (dfr col a) | dfr collect
```
Let's try something more complicated and create aggregations from a lazy
dataframe
```nu
โฏ let a = ( [[name value]; [one 1] [two 2] [one 1] [two 3]] | dfr into-lazy )
โฏ $a |
โ dfr group-by name |
โ dfr agg [
โ (dfr col value | dfr sum | dfr as sum)
โ (dfr col value | dfr mean | dfr as mean)
โ ] | dfr collect
โญโโโโฌโโโโโโโฌโโโโโโฌโโโโโโโฎ
โ # โ name โ sum โ mean โ
โโโโโผโโโโโโโผโโโโโโผโโโโโโโค
โ 0 โ two โ 5 โ 2.50 โ
โ 1 โ one โ 2 โ 1.00 โ
โฐโโโโดโโโโโโโดโโโโโโดโโโโโโโฏ
```
And we could join on a lazy dataframe that hasn't being collected. Let's join
the resulting group by to the original lazy frame
```nu
โฏ let a = ( [[name value]; [one 1] [two 2] [one 1] [two 3]] | dfr into-lazy )
โฏ let group = ($a
โ | dfr group-by name
โ | dfr agg [
โ (dfr col value | dfr sum | dfr as sum)
โ (dfr col value | dfr mean | dfr as mean)
โ ])
โฏ $a | dfr join $group name name | dfr collect
โญโโโโฌโโโโโโโฌโโโโโโโโฌโโโโโโฌโโโโโโโฎ
โ # โ name โ value โ sum โ mean โ
โโโโโผโโโโโโโผโโโโโโโโผโโโโโโผโโโโโโโค
โ 0 โ one โ 1 โ 2 โ 1.00 โ
โ 1 โ two โ 2 โ 5 โ 2.50 โ
โ 2 โ one โ 1 โ 2 โ 1.00 โ
โ 3 โ two โ 3 โ 5 โ 2.50 โ
โฐโโโโดโโโโโโโดโโโโโโโโดโโโโโโดโโโโโโโฏ
```
As you can see lazy frames are a powerful construct that will let you query
data using a flexible syntax, resulting in blazing fast results.
## Dataframe commands
So far we have seen quite a few operations that can be done using `DataFrame`s
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.
::: warning
This list may be outdated. To get the up-to-date command list, see
[Dataframe](/commands/categories/dataframe.md)
[Lazyframe](/commands/categories/lazyframe.md) and
[Dataframe Or Lazyframe](/commands/categories/dataframe_or_lazyframe.md)
command categories.
:::
| Command Name | Applies To | Description | Nushell Equivalent |
| --------------- | --------------------------- | -------------------------------------------------------------------------- | ----------------------------- |
| aggregate | DataFrame, GroupBy, Series | Performs an aggregation operation on a dataframe, groupby or series object | math |
| all-false | Series | Returns true if all values are false | |
| all-true | Series | Returns true if all values are true | all |
| arg-max | Series | Return index for max value in series | |
| arg-min | Series | Return index for min value in series | |
| arg-sort | Series | Returns indexes for a sorted series | |
| arg-true | Series | Returns indexes where values are true | |
| arg-unique | Series | Returns indexes for unique values | |
| count-null | Series | Counts null values | |
| count-unique | Series | Counts unique value | |
| drop | DataFrame | Creates a new dataframe by dropping the selected columns | drop |
| drop-duplicates | DataFrame | Drops duplicate values in dataframe | |
| drop-nulls | DataFrame, Series | Drops null values in dataframe | |
| dtypes | DataFrame | Show dataframe data types | |
| filter-with | DataFrame | Filters dataframe using a mask as reference | |
| first | DataFrame | Creates new dataframe with first rows | first |
| get | DataFrame | Creates dataframe with the selected columns | get |
| group-by | DataFrame | Creates a groupby object that can be used for other aggregations | group-by |
| is-duplicated | Series | Creates mask indicating duplicated values | |
| is-in | Series | Checks if elements from a series are contained in right series | in |
| is-not-null | Series | Creates mask where value is not null | |
| is-null | Series | Creates mask where value is null | `<column_name> == null` |
| is-unique | Series | Creates mask indicating unique values | |
| join | DataFrame | Joins a dataframe using columns as reference | |
| last | DataFrame | Creates new dataframe with last rows | last |
| ls-df | | Lists stored dataframes | |
| melt | DataFrame | Unpivot a DataFrame from wide to long format | |
| not | Series Inverts boolean mask | |
| open | | Loads dataframe form csv file | open |
| pivot | GroupBy | Performs a pivot operation on a groupby object | pivot |
| rename | Dataframe, Series | Renames a series | rename |
| sample | DataFrame | Create sample dataframe | |
| select | DataFrame | Creates a new dataframe with the selected columns | select |
| set | Series | Sets value where given mask is true | |
| set-with-idx | Series | Sets value in the given index | |
| shift | Series | Shifts the values by a given period | |
| show | DataFrame | Converts a section of the dataframe to a Table or List value | |
| slice | DataFrame | Creates new dataframe from a slice of rows | |
| sort-by | DataFrame, Series | Creates new sorted dataframe or series | sort |
| take | DataFrame, Series | Creates new dataframe using the given indices | |
| to csv | DataFrame | Saves dataframe to csv file | to csv |
| into df | | Converts a pipelined Table or List into Dataframe | |
| dummies | DataFrame | Creates a new dataframe with dummy variables | |
| to parquet | DataFrame | Saves dataframe to parquet file | |
| unique | Series | Returns unique values from a series | uniq |
| value-counts | Series | Returns a dataframe with the counts for unique values in series | |
| where | DataFrame | Filter dataframe to match the condition | where |
| with-column | DataFrame | Adds a series to the dataframe | `insert <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.
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.
|