# Count number of substrings in dataframe column

**URL:** https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036
**Category:** Data
**Created:** [February 13, 2018, 3:26pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036 "2018-02-13T15:26:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)
#### Post date: [February 13, 2018, 3:26pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/1 "2018-02-13T15:26:25Z")

</div>

I have a dataset (read in from a CSV). I’m trying to count how many observations contain a substring (for simplicity, let’s call it “X”). In R, I can do this

```julia
sum(stringr::str_count(data$CITY, "X")) > 0

```

I’m having trouble figuring out what the analagous Julia code would be. I’ve tried

```julia
sum(contains.(data[:CITY], "X")) > 0

```

But the LHS gives me a `Nullable` array that can’t be compared with `0`. I can force `0` to be Nullable (e.g. `Nullable(0)`, but then I can’t use this comparison in an `if` statement e.g.

```julia
if sum(contains.(data[:CITY], "X")) > Nullable(0)
    # do some stuff
end

```

Any thoughts on the appropriate way to make this count and compare to another number?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [February 13, 2018, 3:38pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/2 "2018-02-13T15:38:26Z")

</div>

You appear to be using an obsolete version of DataFrames. I recommend updating DataFrames to the most recent tagged version.

Note that currently missing data is represented by `missing` and currently there is no `contains(::Missing, ::String)` method. The best thing to do is to define `contains(::Missing, ::AbstractString) = missing` for yourself.

Note also that your expression above will return `missing` if `data[:CITY]` contains any missing values. You can use `Missings.skip` to return an iterator which excludes `missing`s.

---

<div class="post-metadata">

### Author: ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)
#### Post date: [February 13, 2018, 3:57pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/3 "2018-02-13T15:57:49Z")

</div>

Thanks @ExpandingMan. I see that I’m using 0.10.1 for DataFrames in spite of running `Pkg.update()`. I guess that one of the dependents is requiring that version of DataFrames. Is it possible to force an upgrade to DataFrames? I’ve tried `Pkg.pin("DataFrames", v"0.11.5")` but get the following error

ERROR: GitError(Code:ENOTFOUND, Class:Reference, Revspec ‘06c352352fde5e90d738959e54d  
43efb255bcc15’ not found.)

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 13, 2018, 4:12pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/4 "2018-02-13T16:12:47Z")

</div>

You cannot pin a package to a version you don’t have yet. You can maybe use [this trick](https://discourse.julialang.org/t/how-to-find-package-preventing-others-from-updating/8235) to figure out what’s holding DataFrames from upgrading, see also [this thread](https://discourse.julialang.org/t/how-to-upgrade-from-dataframes-0-10-1-to-0-11-3/8243). Then if you don’t need them, you could remove the packages that are holding you back and proceed to update DataFrames. Gadfly or ExcelReaders are likely candidates of packages that have not yet become compatible with the new DataFrames, but maybe in your case it’s something else.

As per the problem per se, once you update to DataFrames 0.11, you can do either:

`count(t -> contains(t, "X"), data[:CITY])`

or, if you have missing data you want to skip in the `CITY` column:

`count(t -> contains(t, "X"), skipmissing(data[:CITY]))`

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [February 13, 2018, 4:33pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/5 "2018-02-13T16:33:31Z")

</div>

Yeah, unfortunately the package manager is in really poor shape right now and problems like that are common. The good news is that it is going to be completely overhauled in 0.7. For now you might want to just use git to clone packages directly. I also recommend doing `Pkg.rm` on a bunch of packages you haven’t been using recently. Sometimes it seems that cleaning old things up a bit and reinstalling helps. Lastly, try a `Pkg.update()`.

---

<div class="post-metadata">

### Author: ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)
#### Post date: [February 13, 2018, 5:56pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/6 "2018-02-13T17:56:25Z")

</div>

`git clone` is a bit of a last resort, but I have to admit that I manually uninstalled a large chunk of my `.julia/v0.6` folder to update DataFrames in a timely fashion. Totally worth it though, I think the new version is a major improvement.

Now it should be a bit easier though, I can only think of Gadfly and ExcelReaders as packages who haven’t updated yet and they both have open PRs.

---

<div class="post-metadata">

### Author: ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)
#### Post date: [February 13, 2018, 5:59pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/7 "2018-02-13T17:59:51Z")

</div>

You can also work with DataFrames 0.10, just use `readtable` to read CSV files rather than CSV.jl with that version.

---

<div class="post-metadata">

### Author: ![jacobcvt12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobcvt12/32/2695_2.png) [@jacobcvt12](https://discourse.julialang.org/u/jacobcvt12)
#### Post date: [February 13, 2018, 6:21pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/8 "2018-02-13T18:21:26Z")

</div>

Thanks everyone - upgrading `DataFrames` worked. Unfortunately - I ended up just uninstalling every package to do so. Looking forward to the overhaul of the package manager 😉

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [February 13, 2018, 11:07pm UTC](https://discourse.julialang.org/t/count-number-of-substrings-in-dataframe-column/9036/9 "2018-02-13T23:07:38Z")

</div>

The [Query.jl](https://github.com/davidanthoff/Query.jl) version of this looks as follows:

```julia
 df |> @filter(contains(_.city, "n")) |> @count()

```

At least once [this](https://github.com/JuliaLang/METADATA.jl/pull/13352) is merged, this example exposed a bug in Query.jl that I just fixed 🙂
