# On iterating through columns of a data frame and check if they contain String values

**URL:** <https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093>\
**Category:** General Usage\
**Tags:** type, dataframes, data\_structures\
**Created:** [April 23, 2020, 4:11pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093 "2020-04-23T16:11:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abianco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abianco/32/12992_2.png) [@abianco](https://discourse.julialang.org/u/abianco)\
**Post date:** [April 23, 2020, 4:11pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093/1 "2020-04-23T16:11:47Z")

</div>

I am still new to Julia and I am a bit confused about how loop through the columns of a data frame and check their type. I have looked into the documentation and couldn’t find any clarification yet, so I am posting this here.

For the sake of discussion, let’s say I want to check the type of each column in a data frame and perform an operation on those that are not String (perhaps I should say that don’t contain elements of type String). Would the following snippet be correct (in Julia v1.4.1 with DataFrames v0.20.2)?

```julia
using DataFrames

df = DataFrame(text = ["a", "b", "c"], num1 = [1, 3, 5], num2 = [2.0, 4.0, 6.0])

for nm in names(df)
    if eltype(df[!, nm]) != String
        df[!, nm] = df[!, nm]./100
    end
end

```

What would be the best practice to accomplish this?

Incidentally, and I beg pardon in advance for asking this in the wrong category, in VS Code v1.44.2, the word “df” in `names(df)`, `eltype(df[!, nm])` and `df[!, nm]./100` has an underlying yellow/orange wavy line (the editor claims “`Missing reference: df Julia(Julia)`”), as well as it shows a wawy reddish underlying line from `String` to the equal after `df[!, nm]` in the next line (in this case it throws a “`Parsing error Julia(Julia)`” message). Why is that the case? Are these warnings/errors? The code runs just fine without any message from Julia though…

```julia
versioninfo()
Julia Version 1.4.1
Commit 381693d3df* (2020-04-14 17:20 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, skylake)

```

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [April 28, 2020, 6:17pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093/2 "2020-04-28T18:17:16Z")

</div>

I think you’ve got the key best practice idea as far as writing the data in place.

In VS Code all you may need to do is include the Julia Language Support extension.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 28, 2020, 6:32pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093/3 "2020-04-28T18:32:03Z")

</div>

Yep that’s it. There is an iterator `eachcol` and also a function `mapcols` that can help you do this. Unfortunately, there is no `mapnumericcols`, so you always need to check column types as in your example (so it basically isn’t really any easier with those functions).

---

<div class="post-metadata">

**Author:** ![abianco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abianco/32/12992_2.png) [@abianco](https://discourse.julialang.org/u/abianco)\
**Post date:** [April 28, 2020, 8:21pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093/4 "2020-04-28T20:21:50Z")

</div>

Thank you @oliver and @tbeason so much for your answers. I assume I can/should close this post then (how?).

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 28, 2020, 8:49pm UTC](https://discourse.julialang.org/t/on-iterating-through-columns-of-a-data-frame-and-check-if-they-contain-string-values/38093/5 "2020-04-28T20:49:54Z")

</div>

You can just mark one of our replies as the “Solution”.
