Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to fetch top N rows of dataframe in Julia : UndefVarError: head not defined

Tags:

julia

I am new to Julia, I want to see the first 5 rows of data frame, but when i am writing below code

 head(df,5)

I am getting

 UndefVarError: head not defined
like image 428
Zeeshan Avatar asked Nov 30 '20 13:11

Zeeshan


1 Answers

head is available in e.g. R but not in Julia. First - note that Julia has a nice data frame printing system out of the box that crops things to fit in the terminal window, so you do not need to subset your data frame to see its head and tail. Here is an example:

julia> df = DataFrame(rand(100, 100), :auto)
100×100 DataFrame
 Row │ x1         x2          x3          x4         x5         x6        x7     ⋯
     │ Float64    Float64     Float64     Float64    Float64    Float64   Float6 ⋯
─────┼────────────────────────────────────────────────────────────────────────────
   1 │ 0.915485   0.176254    0.381047    0.710266   0.597914   0.177617  0.4475 ⋯
   2 │ 0.58495    0.551726    0.464703    0.630956   0.476727   0.804854  0.7908
   3 │ 0.123723   0.183817    0.986624    0.306091   0.202054   0.148579  0.3433
   4 │ 0.558321   0.117478    0.187091    0.482795   0.0718985  0.807018  0.9463
   5 │ 0.771561   0.515823    0.830598    0.0742368  0.0831569  0.818487  0.4912 ⋯
   6 │ 0.139018   0.182928    0.00129572  0.0439561  0.0929167  0.264609  0.1555
   7 │ 0.16076    0.404707    0.0300284   0.665413   0.681704   0.431746  0.3460
   8 │ 0.149331   0.132869    0.237446    0.599701   0.149257   0.70753   0.7687
  ⋮  │     ⋮          ⋮           ⋮           ⋮          ⋮         ⋮          ⋮  ⋱
  93 │ 0.912703   0.98395     0.133307    0.493799   0.76125    0.295725  0.9249 ⋯
  94 │ 0.153175   0.339036    0.685642    0.355421   0.365252   0.434604  0.1515
  95 │ 0.780877   0.225312    0.511122    0.0506186  0.108054   0.729219  0.5275
  96 │ 0.132961   0.348176    0.619712    0.791334   0.052787   0.577896  0.6696
  97 │ 0.904386   0.938876    0.988184    0.831708   0.699214   0.627366  0.4320 ⋯
  98 │ 0.0295777  0.704879    0.905364    0.142231   0.586725   0.584692  0.9546
  99 │ 0.848715   0.177192    0.544509    0.771653   0.472267   0.584306  0.0089
 100 │ 0.81299    0.00540772  0.107315    0.323288   0.592159   0.1297    0.3383
                                                    94 columns and 84 rows omitted

Now if you need to fetch first 5 rows of your data frame and create a new data frame then use the first function that is defined in Julia Base:

julia> first(df, 5)
5×100 DataFrame
 Row │ x1        x2        x3        x4         x5         x6        x7        x ⋯
     │ Float64   Float64   Float64   Float64    Float64    Float64   Float64   F ⋯
─────┼────────────────────────────────────────────────────────────────────────────
   1 │ 0.915485  0.176254  0.381047  0.710266   0.597914   0.177617  0.447533  0 ⋯
   2 │ 0.58495   0.551726  0.464703  0.630956   0.476727   0.804854  0.790866  0
   3 │ 0.123723  0.183817  0.986624  0.306091   0.202054   0.148579  0.343316  0
   4 │ 0.558321  0.117478  0.187091  0.482795   0.0718985  0.807018  0.946342  0
   5 │ 0.771561  0.515823  0.830598  0.0742368  0.0831569  0.818487  0.491206  0 ⋯
                                                                93 columns omitted

In general the design of DataFrames.jl is that we limit the number of new function names as much as possible and reuse what is defined in Julia Base if possible. This is one example of such a situation. This way users have less things to learn.

like image 79
Bogumił Kamiński Avatar answered Oct 27 '22 05:10

Bogumił Kamiński