Issue
Assumptions: I have a Julia DataFrame with a column titled article_id
.
Normally, I can declare a DataFrame using some syntax like df = DataFrame(CSV.File(dataFileName; delim = ","))
. If I wanted to get the column pertaining to a known attribute, I could do something like df.article_id
. I could also index that specific column by doing df."article_id"
.
However, if I created a string and assigned it to the value of article_id
, such as str = "article_id"
, I cannot index the dataframe via df.str
: I get an error by doing so. This makes sense, as str
is not an attribute of the DataFrame, yet the value of str
is an attribute of the DataFrame. How can I index the DataFrame to get the column corresponding to the value of str
? I'm looking for some syntax similar to df.valueof(str)
.
Are there any solutions to this?
Solution
From the DataFrames.jl manual's "Getting started" page:
Columns can be directly (i.e. without copying) accessed via df.col, df."col", df[!, :col] or df[!, "col"]. The two latter syntaxes are more flexible as they allow passing a variable holding the name of the column, and not only a literal name.
So you can write df[!, str]
, and that will be equivalent to df.article_id
if str == "article_id"
.
The Indexing section of the manual goes into even more detail, for when you need more advanced types of indexing or want a deeper understanding of the options.
Answered By - Sundar R Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.