Reputation: 171
I have a dataframe with some columns,sometimes it can be : [Type_House, Name, Location]
.
And sometimes it can be: [Type_Build, Name, Location]
There is a way to acess this dataframe column Type dynamically, like?
colName = "House"
dataframe.Type_colName
Thanks.
Upvotes: 0
Views: 148
Reputation: 14695
If you want to access the column that starts with Type_, you can use the names
function this way:
julia> df = DataFrame( Type_Build = ["foo", "bar"], Name = ["A", "B"])
2×2 DataFrame
Row │ Type_Build Name
│ String String
─────┼────────────────────
1 │ foo A
2 │ bar B
julia> names(df, startswith("Type_"))
1-element Vector{String}:
"Type_Build"
To access the values in the column, you can use that to index into the dataframe:
julia> df[!, names(df, startswith("Type_"))]
2×1 DataFrame
Row │ Type_Build
│ String
─────┼────────────
1 │ foo
2 │ bar
Upvotes: 1
Reputation: 108
As indicated by @jling but specific to your question it would be:
> colName = "House"
> df[!, "Type_"*colName]
or
> getproperty(df, "Type_"*colName)
then you can just change colName="Build"
to select the other column.
Upvotes: 2
Reputation: 2301
if you have
colName = "House"
you can access the column with
df[!, colName]
and from there you can use typeof()
or eltype()
to get the type or element type of that column
Upvotes: 2