How to extract substring of a Julia Dataframe column

Hi, I may be overthinking this one, but all I’m trying to do is turn a column into a specific indexed substring of that column.

df = DataFrame(number=[123456,134553])
df[:number] = string.(df[:number]

All I want to do is return the first 3 digits of each row value for the numbers column, in this case “123” and “134”.

Thanks

julia> getindex.(string.(a), Ref(1:3))
2-element Array{String,1}:
 "123"
 "134"

Note, you should be using the non-deprecated indexing behavior for data frames, i.e. df[:, :number] or df.number

julia> df.str_sub_number = SubString.(string.(df.number), 1, 3)

If you don’t want to worry about SubStrings, do

julia> df.str_sub_number = getindex.(string.(df.number), Ref(1:3))
2 Likes

cool thank you!