# DataFrames vs ndsparse for indexed data

**URL:** https://discourse.julialang.org/t/dataframes-vs-ndsparse-for-indexed-data/60553
**Category:** Performance
**Created:** [May 5, 2021, 12:22am UTC](https://discourse.julialang.org/t/dataframes-vs-ndsparse-for-indexed-data/60553 "2021-05-05T00:22:45Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jekyllstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jekyllstein/32/4157_2.png) [@jekyllstein](https://discourse.julialang.org/u/jekyllstein)
#### Post date: [May 5, 2021, 12:22am UTC](https://discourse.julialang.org/t/dataframes-vs-ndsparse-for-indexed-data/60553/1 "2021-05-05T00:22:45Z")

</div>

With the release of DataFrames 1.0 I was curious about the performance benefits of accessing data with it vs something like an indexed table. The example would be having tabular data with a column and wanting to return a subtable of items that match the key column.

```julia
using DataFrames
using JuliaDB
df = DataFrame(sex = ["male", "male", "female", "male", "female", "male"], age = [20, 14, 65, 34, 23, 67])
df[df.sex .== "male", :]

tbl = ndsparse(df)
tbl[("male",)]

```

When I try benchmarking on a laptop I get the following results but I am not sure if it is simply because of the tiny size of this example

```nohighlight
julia> @btime $df[$df.sex .== "male", :]
  1.450 μs (21 allocations: 1.58 KiB)
4×2 typename(DataFrame)
│ Row │ sex │ age │
│ │ String │ Int64 │
├─────┼────────┼───────┤
│ 1 │ male │ 20 │
│ 2 │ male │ 14 │
│ 3 │ male │ 34 │
│ 4 │ male │ 67 │
julia> @btime $tbl[("male",)]
  6.778 μs (80 allocations: 4.44 KiB)
1-d NDSparse with 4 values (1 field named tuples):
sex │ age
───────┼────
"male" │ 20
"male" │ 14
"male" │ 34
"male" │ 67

```

---

<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: [May 5, 2021, 9:44am UTC](https://discourse.julialang.org/t/dataframes-vs-ndsparse-for-indexed-data/60553/2 "2021-05-05T09:44:36Z")

</div>

What’s the question? I would try this with larger examples to see whether the differences are meaningful.

Also note that with DataFrames, you can do `@view df[df.sex .== "male", :]` which allocates less and is around twice as fast on my machine.
