Home > front end >  set_index() on Julia dataframe
set_index() on Julia dataframe

Time:10-19

I am looking for a function like .set_index() in python at Julia dataframe.

I've searched and find out NamedArray can give similar result with .set_index() in Python as below:

n = NamedArray(rand(2,4))
setnames!(n, ["one", "two"], 1)  
n["one", 2:3]
n["two", :] = 11:14
n[Not("two"), :] = 4:7

Out[10]
2×4 Named Matrix{Float64}
A ╲ B │    1     2     3     4
──────┼───────────────────────
one   │  4.0   5.0   6.0   7.0
two   │ 11.0  12.0  13.0  14.0

However, NamedArray returns as matrix format, and I could not find function injulia dataframe. Is there any function like .set_index()?

Like this is what I expect :

>>> df
        1        2      3       4
value  Int64    Float64 Float64 Float64              
one    84       64      42      77
two    24       90      8       33

CodePudding user response:

There is no function similar to set_index in DataFrames.jl. The recommended thing is to add this data as a column of a data frame. Then you can e.g. groupby the data by this column to have a quick lookup.

If you provided more information about what you need the row index for I can comment how this can be done in DataFrames.jl?

CodePudding user response:

One way is,

A = Dict("a" => 1, "b" => 2)

Then,

setindex!(A, 11, "c")
df = DataFrame(A)
1×3 DataFrame
 Row │ a      b      c
     │ Int64  Int64  Int64
─────┼─────────────────────
   11      2     11
  • Related