# If I have a DataFrame and want to get \`unique\` columns but treat each column as a

**URL:** <https://discourse.julialang.org/t/if-i-have-a-dataframe-and-want-to-get-unique-columns-but-treat-each-column-as-a/55852>\
**Category:** General Usage\
**Created:** [February 23, 2021, 2:14pm UTC](https://discourse.julialang.org/t/if-i-have-a-dataframe-and-want-to-get-unique-columns-but-treat-each-column-as-a/55852 "2021-02-23T14:14:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [February 23, 2021, 2:14pm UTC](https://discourse.julialang.org/t/if-i-have-a-dataframe-and-want-to-get-unique-columns-but-treat-each-column-as-a/55852/1 "2021-02-23T14:14:32Z")

</div>

if I have a DataFrame and want to get `unique` columns but treat each column as a separate entity, what is the best way to do it?  
MATLAB has a convenience argument `unique(A(:,1:2),'rows')` (yuk!) … but I can’t find an equivalent way to do it in Julia

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1614089654196400?thread_ts=1614089654.196400&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<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:** [February 23, 2021, 3:15pm UTC](https://discourse.julialang.org/t/if-i-have-a-dataframe-and-want-to-get-unique-columns-but-treat-each-column-as-a/55852/2 "2021-02-23T15:15:26Z")

</div>

`unique` in DataFrames works by row. You could do

```julia

julia> df = DataFrame(a = [1, 2], b = [3, 4], c = [1, 2]);

julia> unique_cols = unique(i -> i[2], pairs(eachcol(df)))
2-element Array{Pair{Symbol,AbstractArray{T,1} where T},1}:
 :a => [1, 2]
 :b => [3, 4]

julia> DataFrame(unique_cols)
2×2 DataFrame
 Row │ a b     
     │ Int64 Int64 
─────┼──────────────
   1 │ 1 3
   2 │ 2 4

```

`unique` has a version which applies a function to each element of the iterator and finds unique inputs for after the function has been applied.

`pairs(eachcol(df))` is an iterator of `colname => vector` pairs.

You apply the function `i -> i[2]` to each of these `colname => vector` pairs because you only want to compare the vectors. Obviously each column has a unique name.

you are left with a vector of pairs. The `DataFrame` constructor can work with a vector of `colname => vector` pairs. And you are done
