# Create a new column that has a list of items of a second column, with a condition from a third column

**URL:** <https://discourse.julialang.org/t/create-a-new-column-that-has-a-list-of-items-of-a-second-column-with-a-condition-from-a-third-column/83206>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [June 22, 2022, 9:40pm UTC](https://discourse.julialang.org/t/create-a-new-column-that-has-a-list-of-items-of-a-second-column-with-a-condition-from-a-third-column/83206 "2022-06-22T21:40:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Mac\_Donagh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan_mac_donagh/32/31798_2.png) [@Juan\_Mac\_Donagh](https://discourse.julialang.org/u/Juan_Mac_Donagh)\
**Post date:** [June 22, 2022, 9:40pm UTC](https://discourse.julialang.org/t/create-a-new-column-that-has-a-list-of-items-of-a-second-column-with-a-condition-from-a-third-column/83206/1 "2022-06-22T21:40:20Z")

</div>

Hi, I have a simple problem: I have a DataFrame that looks like this:

```julia
df = DataFrame()

df.sum = [[200,0,1,1,0], [0,0,0], [0,1,1,1,1,0,0]]
df.score= [[1,2,22,25,5], [1,1,2], [1,24,54,89,12,1,2]]
df.id = ["1", "2", "3"]

```

And I want to generate a column that has the _values_ of df.score that coincide with the values that are \>+ 1 in the df.sum column. This is my expected result:

```julia
df.x1 = [[1,22,25], [], [24,54,89,12]]

```

Where the second row should be empty, because there are no values greater than 1 in df.sum[1].

This is the code that I’ve been tinkering with (I took it from [Change column to row using conditions](https://discourse.julialang.org/t/change-coulumn-to-row-using-conditions/83175)), but I can’t get my head around on how to modify it so it works:

```julia
	dv2 = df.sum;
	for (i, v) in enumerate(dv2)
		for (j,h) in enumerate(v) 
    		if h >= 1
        		df[:,:new_col] .= df.Score[i][j]
			end
    	end
	end

```

The problem with this that it gives me a column that is composed only by the number 12 (`[12, 12, 12]`) in every row, and that is the last value that is greater than 1 of the last column. I don’t know why this happens, but I know that it has to do with how I the loop is written

Any help is welcome, thanks a lot!

---

<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:** [June 22, 2022, 9:54pm UTC](https://discourse.julialang.org/t/create-a-new-column-that-has-a-list-of-items-of-a-second-column-with-a-condition-from-a-third-column/83206/2 "2022-06-22T21:54:11Z")

</div>

I don’t understand what the actual transformation you are trying to do is, but here is an answer using DataFramesMeta.jl

```julia
julia> t = @rtransform df :x1 = begin 
           better_than = :score .> (:sum .+ 1)
           :score[better_than]
       end;

julia> @select t :id :x1
3×2 DataFrame
 Row │ id x1                  
     │ String Array…              
─────┼─────────────────────────────
   1 │ 1 [2, 22, 25, 5]
   2 │ 2 [2]
   3 │ 3 [24, 54, 89, 12, 2]

```

EDIT: you want

```julia
julia> t = @rtransform df :x1 = begin 
           better_than = :sum .>= 1
           :score[better_than]
       end;

```
