So I’m writing some code where I need to access some properties of some machine register. I want to store both the value the register it’s initializated to and its binary weight. Using a DataFrame and the count_ones function, I could produce this:
However, I’d like now to access the data I just populated, hopefully with something like
weight = df["r2", :Weight]
but IIUC, DataFrames don’t allow that. Is there a way I can do this? I know Dicts could be indexed using a String, but in that case I couldn’t be able to store multiple data (columns) for each register, right? Is there a way I can access a table indexing it using Strings?
#Get a boolean vector that's true where the string matches
r2_indices = df[:, :Register] .== "r2"
#use that to index the dataframe
df[r2_indices, :Weight]
I’m sure there is a cleaner syntax for this with select(), but this should get the job done!
If you want a single lookup then Boolean mask it the standard approach to do it. If you want multiple lookups then use the groupby approach. A small comment is that you need to write:
Thanks ! But I don’t understand @bkamins , do you mean that using groupby has an initial heavier overhead, and then if I need only a single lookup then the boolean vector approach is faster? Because otherwise I don’t see why would the vector be “simpler”, if that’s what you meant
But it gets exported to CSV with the tuple converted to a String, and moreover I suspect the performance is close if not equal to the groupbyed DataFrame, thus making the latter preferable. Is this correct?