# How to subset dataframe by multiple rows with same value?

**URL:** <https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294>\
**Category:** Statistics\
**Tags:** question, package, data, dataframes\
**Created:** [January 30, 2021, 7:53pm UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294 "2021-01-30T19:53:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [January 30, 2021, 7:53pm UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/1 "2021-01-30T19:53:31Z")

</div>

Hi  
I want to subset a dataframe(df) by multiple rows with value (10004).  
I tried the command, but it does not work, I am wondering why ?

```julia
df[in([10004]).(df.HWG), :]

```

> 28,960 rows × 11 columns (omitted printing of 2 columns)

| | HWG | X | X.1 | X.2 | X.3 | X.4 | X.5 | Herd.born.in | GenotypedDate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| | String | Int64 | Int64 | Int64 | Int64 | Int64 | Int64 | String | String |
| 1 | 10004 | 0 | 410240170 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 2 | 10004 | 0 | 412396339 | 412301476 | 409446588 | 2 | -9 | 10004 | 01.12.2017 |
| 3 | 10004 | 0 | 412442014 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 4 | 10004 | 0 | 412409256 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 5 | 10004 | 0 | 412419664 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 6 | 10004 | 0 | 412442177 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 7 | 10004 | 0 | 412556442 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 8 | 10004 | 0 | 412502732 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 9 | 10004 | 0 | 412490450 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 10 | 10004 | 0 | 412788155 | 0 | 0 | 2 | -9 | 10004 | 20.12.2018 |
| 11 | 10004 | 0 | 410103042 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 12 | 10004 | 0 | 412532939 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 13 | 10004 | 0 | 411394744 | 0 | 0 | 2 | -9 | 10004 | 14.12.2018 |
| 14 | 10005 | 0 | 411203394 | 412301753 | 409435638 | 2 | -9 | 10005 | 24.08.2018 |
| 15 | 10005 | 0 | 410101642 | 412300972 | 410144275 | 2 | -9 | 10005 | 24.08.2018 |
| 16 | 10005 | 0 | 410786267 | 412301772 | 411910808 | 2 | -9 | 10005 | 24.08.2018 |
| 17 | 10005 | 0 | 412551400 | 412301966 | 409336495 | 2 | -9 | 10005 | 24.08.2018 |
| 18 | 10005 | 0 | 408990326 | 412301218 | 409435638 | 2 | -9 | 10005 | 24.08.2018 |
| 19 | 10005 | 0 | 412532262 | 412301953 | 409951758 | 2 | -9 | 10005 | 24.08.2018 |
| 20 | 10005 | 0 | 412524592 | 412301946 | 412012310 | 2 | -9 | 10005 | 24.08.2018 |
| 21 | 10005 | 0 | 408766913 | 412301860 | 410744756 | 2 | -9 | 10630 | 24.08.2018 |
| 22 | 10005 | 0 | 412547148 | 412301494 | 408742355 | 2 | -9 | 10005 | 24.08.2018 |
| 23 | 10005 | 0 | 412524860 | 412301964 | 411028826 | 2 | -9 | 10005 | 24.08.2018 |
| 24 | 10005 | 0 | 412482552 | 412301949 | 408884862 | 2 | -9 | 10005 | 24.08.2018 |

---

<div class="post-metadata">

**Author:** ![DaymondLing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daymondling/32/8474_2.png) [@DaymondLing](https://discourse.julialang.org/u/DaymondLing)\
**Post date:** [January 30, 2021, 8:16pm UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/2 "2021-01-30T20:16:21Z")

</div>

There is a type mismatch. HWG is a String, [10004] is a an Array of Int64, therefore the resulting BitArray is all false. in([“10004”]) will give the correct answer.

To test against a single value, `df[df.hwg .== "10004", :]` should work.  
To test against a set of values, `df.[df.hwg .∈ (["10004", "10005"],), :]` should work.

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [January 30, 2021, 8:27pm UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/3 "2021-01-30T20:27:38Z")

</div>

thanks you for helping,

the command for single value works, but second one got error

> syntax: invalid syntax “df.[(df.hwg .∈ ([“10004”, “10005”],), :]” around In[86]:1

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 31, 2021, 7:49am UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/4 "2021-01-31T07:49:34Z")

</div>

Looks like you have an extra bracket after the opening square bracket?

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [February 1, 2021, 11:05am UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/5 "2021-02-01T11:05:48Z")

</div>

I run this command

> df.[df.hwg .∈ ([“10004”, “10005”],), :]

and gives this error, how can I fix it ?

> syntax: invalid syntax “df.[(df.HWG .∈ ([“10004”, “10005”],)), :]” around In[59]:1

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2021, 11:45am UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/6 "2021-02-01T11:45:53Z")

</div>

Ah sorry I missed the second error in your command - you also have a dot before the opening brackets, it should be `df[` instead of `df.[`

---

<div class="post-metadata">

**Author:** ![Shazman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shazman/32/23093_2.png) [@Shazman](https://discourse.julialang.org/u/Shazman)\
**Post date:** [February 1, 2021, 11:48am UTC](https://discourse.julialang.org/t/how-to-subset-dataframe-by-multiple-rows-with-same-value/54294/7 "2021-02-01T11:48:01Z")

</div>

thank you , now It works 🙂
