# Extracting row of DataFrame directly as NamedTuple?

**URL:** <https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [October 1, 2019, 9:20pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380 "2019-10-01T21:20:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 1, 2019, 9:20pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380/1 "2019-10-01T21:20:09Z")

</div>

Hi all,

I guess that’s a stupid question(?)

I simulated a model with different sets of parameters. To save time (not repeated run the same code with different parameters), I saved the parameters into dataframe row by row.

Now what if I just want to extract a row of it (say row 5) directly as a namedtuple?

Thanks,  
Ethan

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 2, 2019, 12:58pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380/2 "2019-10-02T12:58:35Z")

</div>

Is this what you’re after?

```julia
julia> using DataFrames

julia> df = DataFrame(a = 1:10, b = 1:10)
10×2 DataFrame
│ Row │ a │ b │
│ │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1 │ 1 │ 1 │
│ 2 │ 2 │ 2 │
│ 3 │ 3 │ 3 │
│ 4 │ 4 │ 4 │
│ 5 │ 5 │ 5 │
│ 6 │ 6 │ 6 │
│ 7 │ 7 │ 7 │
│ 8 │ 8 │ 8 │
│ 9 │ 9 │ 9 │
│ 10 │ 10 │ 10 │

julia> copy(df[5, :])
(a = 5, b = 5)

```

Extract from docstring:

```julia
help?> DataFrameRow
(...)
Indexing is one-dimensional like specifying a column of a DataFrame. 
You can also access the data in a DataFrameRow using the getproperty 
and setproperty! functions and convert it to a NamedTuple using the 
copy function.
(...)

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [October 2, 2019, 1:20pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380/3 "2019-10-02T13:20:00Z")

</div>

Also `DataFrameRow` behaves as a mutable `NamedTuple` so most of the time you should be probably fine without a conversion to a `NamedTuple`.

Finally you can easily convert a `DataFrame` into a vector of `NamedTuples`:

```julia
julia> df = DataFrame(rand(4,5))
4×5 DataFrame
│ Row │ x1 │ x2 │ x3 │ x4 │ x5 │
│ │ Float64 │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼──────────┼──────────┼──────────┼───────────┼──────────┤
│ 1 │ 0.770968 │ 0.560951 │ 0.866555 │ 0.415779 │ 0.592464 │
│ 2 │ 0.540743 │ 0.130965 │ 0.753823 │ 0.0484519 │ 0.29774 │
│ 3 │ 0.58207 │ 0.251234 │ 0.839407 │ 0.198445 │ 0.64087 │
│ 4 │ 0.380907 │ 0.639851 │ 0.219417 │ 0.499336 │ 0.549085 │

julia> Tables.rowtable(df)
4-element Array{NamedTuple{(:x1, :x2, :x3, :x4, :x5),NTuple{5,Float64}},1}:
 (x1 = 0.770968397202171, x2 = 0.5609505403103048, x3 = 0.8665553646186814, x4 = 0.4157788264006259, x5 = 0.5924636685911997)
 (x1 = 0.5407429997531747, x2 = 0.13096466013137342, x3 = 0.7538231604145154, x4 = 0.048451924943883506, x5 = 0.2977397808434288)
 (x1 = 0.582069831435476, x2 = 0.25123376929999, x3 = 0.8394071952281461, x4 = 0.1984448483279182, x5 = 0.6408697174304954)
 (x1 = 0.38090740524465483, x2 = 0.6398505002703665, x3 = 0.21941720362172124, x4 = 0.49933624062983384, x5 = 0.5490849304331029)

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 2, 2019, 1:27pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380/4 "2019-10-02T13:27:33Z")

</div>

> [@bkamins](#):
>
> Also `DataFrameRow` behaves as a mutable `NamedTuple` so most of the time you should be probably fine without a conversion to a `NamedTuple` .

Using `DataFrameRow` as a `NamedTuple` is pretty limited - e.g. you cannot unpack it as `f(; row...)`, cannot use tools like merging them or deleting fields, and so on. And performance, of course - getting a field from a `DataFrameRow` is several times slower, than from a plain named tuple.

---

<div class="post-metadata">

**Author:** ![ethanminfang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethanminfang/32/10511_2.png) [@ethanminfang](https://discourse.julialang.org/u/ethanminfang)\
**Post date:** [October 2, 2019, 1:41pm UTC](https://discourse.julialang.org/t/extracting-row-of-dataframe-directly-as-namedtuple/29380/5 "2019-10-02T13:41:43Z")

</div>

Yes. It is. thx!
