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

Add convenience function to look up a single value in a DataFrame #394

Open
nathanrboyer opened this issue Apr 17, 2024 · 2 comments
Open

Comments

@nathanrboyer
Copy link

nathanrboyer commented Apr 17, 2024

It would work similarly to @rsubset but it would automatically call only and extract the desired value.

For example:

julia> df = DataFrame(x = 1:5, y = ["A","A","B","B","B"], z = (1:5)./10)
5×3 DataFrame
 Row │ x      y       z
     │ Int64  String  Float64
─────┼────────────────────────
   11  A           0.1
   22  A           0.2
   33  B           0.3
   44  B           0.4
   55  B           0.5

julia> zvalue = @rsubset(df, :x > 1, :y == "A").z |> only
0.2

julia> zvalue = @lookup(df, :z, :x > 1, :y == "A")
0.2

It could maybe also work without the second argument to return a DataFrameRow.

It was mentioned that this may need to be called rlookup to match rsubset, but I think that is not necessary if the single value method is implemented. (This discussion started in the DataFrames.jl repository: JuliaData/DataFrames.jl#3051 (comment))

Would this function make DataFrames lookups more accessible to newcomers, or is the existing @rsubset functionality good enough?

@nathanrboyer
Copy link
Author

Since this is a meta package, it may not need to look like a function call at all. The syntax could be something more exotic?

zvalue = df[@lookup, :z]  :x > 1, :y == "A"

@pdeffebach
Copy link
Collaborator

That particular syntax isn't actually possible, since @lookup would only apply to the expression inside the brackets.

Moreover, I would definitely want to avoid that kind of exotic syntax. DataFrames.jl doesn't have any data.table style indexing, and I think the initial proposed syntax is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants