# Conditional selection of columns in for loop

**URL:** https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016
**Category:** New to Julia
**Created:** [March 15, 2020, 4:41pm UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016 "2020-03-15T16:41:59Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![meritwagner](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@meritwagner](https://discourse.julialang.org/u/meritwagner)
#### Post date: [March 15, 2020, 4:41pm UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016/1 "2020-03-15T16:41:59Z")

</div>

Hey guys,

Just another beginner-question, I think there is an easy way of doing this:  
I have a Dataframe with 50 columns that each contain 10 million rows of sensor values. I want to resample those, but want to apply differnt resampling algorithms on some of those columns. Since the columnnames are the names of the Sensors I was planning to do this:

for i in df.names  
if occursin(“Temp”, i) == true  
…do this  
end

But I found out that df.names returns type Symbol which can’t be compaired with a string. As far as I understood. Any help would be greatly appreciated. If you have advice how to do it differently and better performancewise I am very happy for suggestions!  
Thanks in advance.

EDIT: Just found out that I can convert the columnames vector to a string vector by doing:  
colnames\_string=[string(col) for col in colnames\_symbol]

Still if there is a better and faster way, I would appreciate hints!

Best  
Merit

---

<div class="post-metadata">

### Author: ![zierenberg](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zierenberg](https://discourse.julialang.org/u/zierenberg)
#### Post date: [March 15, 2020, 5:53pm UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016/2 "2020-03-15T17:53:11Z")

</div>

You could do the string conversion directly in the comparison and would save yourself the extra allocation, because `[...]` actually allocates extra space. So depending on how often you have to iterate over your DataFrame, and on which level you did the allocation, this might help.

Also in my julia version (v1.2) I access the names via names(df), see also the [documentation](https://juliadata.github.io/DataFrames.jl/stable/lib/functions/#Base.names)

So for me worked:

```julia
for i in names(df)
    if occursin("Temp", string(i)) == true
        @show i
    end
end

```

This does of course not answer the question for an alternative faster way 😉

---

<div class="post-metadata">

### Author: ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)
#### Post date: [March 15, 2020, 5:58pm UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016/3 "2020-03-15T17:58:46Z")

</div>

You can also use “string” using the dot notation, it is very readable:

```julia
for i in string.(names)
if occursin(“Temp”, i) == true
…do this
end

```

---

<div class="post-metadata">

### Author: ![meritwagner](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@meritwagner](https://discourse.julialang.org/u/meritwagner)
#### Post date: [March 16, 2020, 8:32am UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016/4 "2020-03-16T08:32:57Z")

</div>

Great, that worked perfectly! Thanks a lot!

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [March 16, 2020, 10:54am UTC](https://discourse.julialang.org/t/conditional-selection-of-columns-in-for-loop/36016/5 "2020-03-16T10:54:16Z")

</div>

normally I would write:

```julia
for col in eachcol(df[!, r"Temp"])
    # do something with col
end

```

in such a case
