Piotr De
Piotr De

Reputation: 157

How to convert comma separated values in a Dataframe cell into an Array in Julia?

I have a Dataframe tile_df with some comma separated id's in each cell. I would like to extract these id's to get a list from.

If I apply

neighbours = split(tile_df.NEIGHBORS, ",")

I get only a 1-element array like this

["01SBD, 01TBE, 01TBF, 30SWJ, 30SXJ, 30SYJ, 30TWK, 30TWL, 30TXL, 30TYK, 30TYL, 31TBE, 31TBF, 60SYJ, 60TYK, 60TYL"]

But what I want is a list of strings and not only one string. Any ideas? Thank you

Upvotes: 2

Views: 537

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69899

If I understand your problem correctly you can do it using broadcasting:

julia> using DataFrames

julia> df = DataFrame(a=["1,2,3", "4,5,6"])
2×1 DataFrame
│ Row │ a      │
│     │ String │
├─────┼────────┤
│ 1   │ 1,2,3  │
│ 2   │ 4,5,6  │

julia> split.(df.a, ",")
2-element Array{Array{SubString{String},1},1}:
 ["1", "2", "3"]
 ["4", "5", "6"]

Upvotes: 2

Related Questions