# DataFrame row to Dict

**URL:** https://discourse.julialang.org/t/dataframe-row-to-dict/44003
**Category:** General Usage
**Tags:** dataframes
**Created:** [July 30, 2020, 7:30pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003 "2020-07-30T19:30:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Samuel\_Collie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_collie/32/7862_2.png) [@Samuel\_Collie](https://discourse.julialang.org/u/Samuel_Collie)
#### Post date: [July 30, 2020, 7:30pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/1 "2020-07-30T19:30:36Z")

</div>

I’d like to query a row of a DataFrame and return the result as a Dict. I was able to do it but the code isn’t pretty (converting Python programmer here). Here is a MWE:

Input DataFrame:

```julia
DataFrame(name=["John", "Sally", "Roger"],
                             age=[54., 34., 79.],
                             children=[0, 2, 4])
3×3 DataFrame
│ Row │ name │ age │ children │
│ │ String │ Float64 │ Int64 │
├─────┼────────┼─────────┼──────────┤
│ 1 │ John │ 54.0 │ 0 │
│ 2 │ Sally │ 34.0 │ 2 │
│ 3 │ Roger │ 79.0 │ 4 │

```

Desired output:

```julia
Dict{String,Real} with 2 entries:
  "age" => 54.0
  "children" => 0

```

Here is my code:

```julia
import DataFrames, Query

df = DataFrame(name=["John", "Sally", "Roger"],
                      age=[54., 34., 79.],
                      children=[0, 2, 4])

john = @from i in df begin
    @where i.name == "John"
    @select {i.age, i.children}
    @collect DataFrame
end
# Query the first row of the returned dataframe
john = john[1,:]

johnDict = Dict("age" => john.age, "children" => john.children)

```

Thanks in advance! I’m new to data-wrangling in Julia.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 30, 2020, 7:33pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/2 "2020-07-30T19:33:26Z")

</div>

```julia
Dict(names(john) .=> values(john))

```

Why do you need a dict, though? a `DataFrameRow` can do everything a dict can.

---

<div class="post-metadata">

### Author: ![Samuel\_Collie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_collie/32/7862_2.png) [@Samuel\_Collie](https://discourse.julialang.org/u/Samuel_Collie)
#### Post date: [July 30, 2020, 7:48pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/3 "2020-07-30T19:48:12Z")

</div>

Well, true. Because I’m trying to learn the syntax I guess (and I hate being stumped).

Thanks for help. Is there a one-liner for the DataFrame query as well? What I have seems excessive.

Thanks again, Sam

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 30, 2020, 7:50pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/4 "2020-07-30T19:50:48Z")

</div>

I’m not too familiar with Query, but that seems like the correct code

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [July 30, 2020, 8:05pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/5 "2020-07-30T20:05:07Z")

</div>

You can call `first` on a DataFrame to get the first row.

I don’t use Query much either but that does seem correct. You could also use Underscores.jl to pipe things from start to finish as well (you use two underscores to refer to the table). It isn’t really any shorter though.

```julia
using DataFrames, Underscores

df = DataFrame(name=["John", "Sally", "Roger"],
                      age=[54., 34., 79.],
                      children=[0, 2, 4])

john = @_ df |> filter(:name => isequal("John"),__) |> select(__,:age,:children) |> first

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [July 30, 2020, 8:08pm UTC](https://discourse.julialang.org/t/dataframe-row-to-dict/44003/6 "2020-07-30T20:08:13Z")

</div>

To add on, this is the solution using just DataFrames and Pipe

```julia
julia> df = DataFrame(name=["John", "Sally", "Roger"],
                             age=[54., 34., 79.],
                             children=[0, 2, 4])
3×3 DataFrame
│ Row │ name │ age │ children │
│ │ String │ Float64 │ Int64 │
├─────┼────────┼─────────┼──────────┤
│ 1 │ John │ 54.0 │ 0 │
│ 2 │ Sally │ 34.0 │ 2 │
│ 3 │ Roger │ 79.0 │ 4 │

julia> using Pipe

julia> @pipe df |>
       filter(r -> r.name == "John", _) |>
       select(_, :age, :children) |>
       first(_)
DataFrameRow
│ Row │ age │ children │
│ │ Float64 │ Int64 │
├─────┼─────────┼──────────┤
│ 1 │ 54.0 │ 0 │

```
