# How to select half the rows from a DataFrame column and return an array of values from another column

**URL:** <https://discourse.julialang.org/t/how-to-select-half-the-rows-from-a-dataframe-column-and-return-an-array-of-values-from-another-column/46486>\
**Category:** New to Julia\
**Tags:** question, dataframes\
**Created:** [September 11, 2020, 11:41pm UTC](https://discourse.julialang.org/t/how-to-select-half-the-rows-from-a-dataframe-column-and-return-an-array-of-values-from-another-column/46486 "2020-09-11T23:41:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yold6](https://avatars.discourse-cdn.com/v4/letter/y/d6d6ee/32.png) [@yold6](https://discourse.julialang.org/u/yold6)\
**Post date:** [September 11, 2020, 11:41pm UTC](https://discourse.julialang.org/t/how-to-select-half-the-rows-from-a-dataframe-column-and-return-an-array-of-values-from-another-column/46486/1 "2020-09-11T23:41:34Z")

</div>

Hi all,

I would like to know how one might partition unique string values from one column of the DataFrame (C) to achieve my goal.

```julia
using Random, DataFrames
C = DataFrame(Team = ["Packers", "Knights", "Bills", "Falcons", "Ravens", "Chiefs", "Titans", "Rams", "Bengals", "Colts", "49ers", "Giants", "Lions", "Steelers", "Jaguars", "Dolphins", "Vikings", "Eagles", "Bears", "Jets", "Cardinals", "Patriots", "Bucs", "Cowboys", "Panthers", "Chargers", "Seahawks", "Browns"], Opponent = ["Bears", "Eagles", "Jets", "Vikings", "Dolphins", "Jaguars", "Browns", "Panthers", "Seahawks", "Chargers", "Bucs", "Cowboys", "Cardinals", "Patriots", "Chiefs", "Ravens", "Falcons", "Knights", "Packers", "Bills", "Lions", "Steelers", "49ers", "Giants", "Rams", "Colts", "Bengals", "Titans"])

```

I would like to divide C into two 1-dimensional arrays A and B such that A contains half of the entries from the column “Team”, and B contains the “Opponent” entry from the same row (in order) as A. So A could be:

```julia
A = "Bills", "Ravens", "Giants", ...

```

to which the corresponding B would be:

```julia
B = "Jets", "Dolphins", "Cowboys", ...

```

The critical points are that all the entries in A and B are unique and correspond to the same row from the original dataframe C. The final order of entries in A and B does not matter.

I’ve tried:

```julia
teams = C[!, :Team]

A = sample(1:(length(teams)/2), Int(length(teams)/2), replace = false)

A = teams[A]

B = setdiff(1:(length(teams)/2), A)

B = teams[B, :]

```

But this fails to maintain the correct pairing between the rows from the “Team” and “Opponent” columns of C. Any insightful input into my dilemma would be much appreciated. Thank you for your time!

---

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [September 12, 2020, 2:11am UTC](https://discourse.julialang.org/t/how-to-select-half-the-rows-from-a-dataframe-column-and-return-an-array-of-values-from-another-column/46486/2 "2020-09-12T02:11:05Z")

</div>

You’re almost there, right?

```julia
using StatsBase

selected = sample(1:size(C,1), size(C,1) ÷ 2, replace=false)
A = C[selected, :Team]
B = C[selected, :Opponent]

```

---

<div class="post-metadata">

**Author:** ![yold6](https://avatars.discourse-cdn.com/v4/letter/y/d6d6ee/32.png) [@yold6](https://discourse.julialang.org/u/yold6)\
**Post date:** [September 16, 2020, 9:25pm UTC](https://discourse.julialang.org/t/how-to-select-half-the-rows-from-a-dataframe-column-and-return-an-array-of-values-from-another-column/46486/3 "2020-09-16T21:25:09Z")

</div>

Thanks [kmundnic](https://discourse.julialang.org/u/kmundnic)!  
That was just the syntax I was looking for. I appreciate it!
