-
Notifications
You must be signed in to change notification settings - Fork 370
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
Changes from all commits
0020b16
b1ec7d7
072fa00
1909a25
c71d325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -877,6 +877,58 @@ package provides interfaces similar to LINQ and [dplyr](https://dplyr.tidyverse. | |
|
||
See the [Data manipulation frameworks](@ref) section for more information. | ||
|
||
#### Selecting rows with `filter` and `subset` | ||
|
||
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=["Alice", "Bob", "Claire", "Dylan"], a='a':'d') | ||
4×3 DataFrame | ||
Row │ x y a | ||
│ Int64 String Char | ||
─────┼───────────────────── | ||
1 │ 1 Alice a | ||
2 │ 2 Bob b | ||
3 │ 3 Claire c | ||
4 │ 4 Dylan d | ||
|
||
julia> df[(df.x .> 2) .& (df.a .== 'c'), : ] | ||
1×3 DataFrame | ||
Row │ x y a | ||
│ Int64 String Char | ||
─────┼───────────────────── | ||
1 │ 3 Claire c | ||
``` | ||
|
||
An alternative formulation, which notably saves on the need to use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we provide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do this - then it should be after |
||
broadcasting syntax via `.` prefixes, uses [`filter`](@ref) or [`filter!`](@ref): | ||
|
||
```jldoctest dataframe | ||
julia> filter([:x, :a] => (x, a) -> x > 2 && a == 'c', df) | ||
1×3 DataFrame | ||
Row │ x y a | ||
│ Int64 String Char | ||
─────┼───────────────────── | ||
1 │ 3 Claire c | ||
``` | ||
|
||
You can also use the [`subset`](@ref) or [`subset!`](@ref) functions that are more | ||
flexible. They use the same syntax as [`select`](@ref) that was discussed in | ||
the previous section). The requirement is that each column transformation | ||
must produce a vector of `Bool` and only the rows from the source data frame | ||
for which all transformations produce `true` are included in the result: | ||
|
||
```jldoctest dataframe | ||
julia> subset(df, :x => ByRow(>(2)), :a => ByRow(==('c'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe start showing the standard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK - I will update it when we settle on the general layout. |
||
1×3 DataFrame | ||
Row │ x y a | ||
│ Int64 String Char | ||
─────┼───────────────────── | ||
1 │ 3 Claire c | ||
``` | ||
|
||
### Summarizing Data | ||
|
||
The `describe` function returns a data frame summarizing the elementary statistics and information about each column: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd put this section above the one presenting
select
andtransform
, which is more complex and goes beyond taking subsets. (Transformations probably deserve a separate section so that we only present columns selection, but that's another story.)That way you don't need to repeat the example: you can just continue with the ones given at the end of the Indexing section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot, as
subset
has the same syntax asselect
so we need to introduce it afterfilter
. We could split the discussion offilter
andsubset
though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah indeed. Let's keep it that way then. But "Taking a Subset" isn't actually a good name for the parent section as it also shows complex transformations. Maybe we should just split each sub-section into its own section, i.e. "Indexing", "Selecting and transforming columns" and "Selecting rows".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.