# Finding the id of all columns containing 1 for a particular row of a DataFrame

**URL:** <https://discourse.julialang.org/t/finding-the-id-of-all-columns-containing-1-for-a-particular-row-of-a-dataframe/58791>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [April 7, 2021, 7:37pm UTC](https://discourse.julialang.org/t/finding-the-id-of-all-columns-containing-1-for-a-particular-row-of-a-dataframe/58791 "2021-04-07T19:37:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)\
**Post date:** [April 7, 2021, 7:37pm UTC](https://discourse.julialang.org/t/finding-the-id-of-all-columns-containing-1-for-a-particular-row-of-a-dataframe/58791/1 "2021-04-07T19:37:59Z")

</div>

I have a DataFrame consisting of many columns and rows, and each entry being simply 1’s or 0’s. For a particular row, I now want to identify all the columns that contain 1. So, I do the following for the first row:

```
findall(x->x == 1,data[1,:]))

```

The result is a Vector(Symbol) containing :x5, :x22, and :x79.

Since my purpose is to use the ids (i.e., the 5, the 22, and the 79) to get information from a related Array, I could do the following instead

```
ids = findall(x->x == 1,convert(Array,data[1,:]))

```

So my question is, can I achieve the same effect without “convert”?

---

<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:** [April 7, 2021, 7:54pm UTC](https://discourse.julialang.org/t/finding-the-id-of-all-columns-containing-1-for-a-particular-row-of-a-dataframe/58791/2 "2021-04-07T19:54:53Z")

</div>

Here is one way of doing it

```julia
julia> df = DataFrame(a = [1, 1], b = [0, 0], c = [0, 1]);

julia> names_dict = Dict(propertynames(df)[i] => i for i in 1:length(propertynames(df)))
Dict{Symbol, Int64} with 3 entries:
  :a => 1
  :b => 2
  :c => 3

julia> df.has_1 = map(eachrow(df)) do row
           getindex.(Ref(names_dict), findall(==(1), row))
       end
2-element Vector{Vector{Int64}}:
 [1]
 [1, 3]

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 7, 2021, 8:56pm UTC](https://discourse.julialang.org/t/finding-the-id-of-all-columns-containing-1-for-a-particular-row-of-a-dataframe/58791/3 "2021-04-07T20:56:39Z")

</div>

```julia
using DataFrames

data = DataFrame(rand(0:1,4,4))
│ Row │ x1 │ x2 │ x3 │ x4 │
│ │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┤
│ 1 │ 1 │ 1 │ 1 │ 0 │
│ 2 │ 0 │ 1 │ 1 │ 0 │
│ 3 │ 0 │ 1 │ 1 │ 0 │
│ 4 │ 0 │ 1 │ 0 │ 0 │

(1:size(data,2))[[d==1 for d in data[1,:]]]
3-element Vector{Int64}:
 1
 2
 3
(1:size(data,2))[[d==1 for d in data[2,:]]]
2-element Vector{Int64}:
 2
 3

```
