5.2 Column Selection

Let’s revisit our example all_grades() data defined in Chapter 4:

  1. df = all_grades()
namegrade
Sally1.0
Bob5.0
Alice8.5
Hank4.0
Bob9.5
Sally9.5
Hank6.0

The DataFramesMeta.jl @select macro is similar to the select DataFrames.jl function. It performs column selection:

  1. @select df :name
name
Sally
Bob
Alice
Hank
Bob
Sally
Hank

We can add as many colums as we want to @select:

  1. @select df :name :grade
namegrade
Sally1.0
Bob5.0
Alice8.5
Hank4.0
Bob9.5
Sally9.5
Hank6.0

To use the column selectors (Section 4.4), you need to wrap them inside $():

  1. @select df $(Not(:grade))
name
Sally
Bob
Alice
Hank
Bob
Sally
Hank

The DataFramesMeta.jl syntax, for some users, is easier and more intuitive than the DataFrames.jl’s minilanguage source => transformation => target. The minilanguage is replaced by target = transformation(source).

Suppose that you want to represent the grades as a number between 0 and 100:

  1. @select df :grade_100 = :grade .* 10
grade_100
10.0
50.0
85.0
40.0
95.0
95.0
60.0

Of course, the .* can be ommited by using the vectorized form of the macro, @rselect:

  1. @rselect df :grade_100 = :grade * 10
grade_100
10.0
50.0
85.0
40.0
95.0
95.0
60.0

5.2 Column Selection - 图1 Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso