Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebased version for #2327 add filter subsetting to docs #2598

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/src/man/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,42 @@ Equivalently, the `in` function can be called with a single argument to create
a function object that tests whether each value belongs to the subset
(partial application of `in`): `df[in([1, 5, 601]).(df.A), :]`.

#### Selecting rows with `filter`

We have seen above how to subset a `DataFrame` to several criteria, involving multiple columns, by supplying a logical vector to the first dimension. For instance, in the following we want to subset to all rows where `x > 2` and where `a == 'c'`:

```jldoctest dataframe
julia> df = DataFrame(:x => 1:4, :y => rand(4), :a => 'a':'d')
4×3 DataFrame
Row │ x y a
│ Int64 Float64 Char
─────┼───────────────────────
1 │ 1 0.830964 a
2 │ 2 0.478479 b
3 │ 3 0.939474 c
4 │ 4 0.742664 d

julia> df[(df.x .> 2) .& (df.a .== 'c'), : ]
1×3 DataFrame
Row │ x y a
│ Int64 Float64 Char
─────┼───────────────────────
1 │ 3 0.939474 c

```

An alternative formulation, which notably saves on the need to use
broadcasting syntax via `.` prefixes, uses [`filter`](@ref) or [`filter!`](@ref):

```jldoctest dataframe
julia> filter([:x, :a] => (x1, x2) -> x1 > 2 && x2 == 'c', df)
1×3 DataFrame
Row │ x y a
│ Int64 Float64 Char
─────┼───────────────────────
1 │ 3 0.939474 c
```

!!! note

As with matrices, subsetting from a data frame will usually return a copy of
Expand Down