5.2 Column Selection

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

df = all_grades()
name grade
Sally 1.0
Bob 5.0
Alice 8.5
Hank 4.0
Bob 9.5
Sally 9.5
Hank 6.0

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

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

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

@select df :name :grade
name grade
Sally 1.0
Bob 5.0
Alice 8.5
Hank 4.0
Bob 9.5
Sally 9.5
Hank 6.0

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

@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:

@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 omitted by using the vectorized form of the macro, @rselect:

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


Support this project
CC BY-NC-SA 4.0 Jose Storopoli, Rik Huijzer, Lazaro Alonso