# Split string to multiple columns

**URL:** https://discourse.julialang.org/t/split-string-to-multiple-columns/26994
**Category:** New to Julia
**Created:** [July 30, 2019, 8:54pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994 "2019-07-30T20:54:52Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [July 30, 2019, 8:54pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/1 "2019-07-30T20:54:52Z")

</div>

Pretty simple, but I’ve been banging my head against it for a while.

I have an array of some thousands of strings representing data files named like “abc-xyz.csv”. I can split on “-” and “.” easy enough, and get a vector of substring arrays.

At most I can coerce this into a vector of arrays of strings. I can’t even get this into a dataframe properly; it comes in as thousands of columns and 3 rows, and there does not appear to be a way to just invert a dataframe.

I’ve loaded the filename array into a dataframe and applied the split, but this just gives me a single column containing 3-element arrays, which I am again stuck with.

This is an issue because the information in the filename string describes the observation and are variables to be used in analysis. The goal is to populate these variables from the filenames, then load the files, each of which contains one observation, and append the data to the row containing the filename.

I’m also curious about the best way to do this, since I could see it getting into a lot of interesting concurrent IO questions. But for now I just want to learn how to not be abused by the type system, thanks.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [July 30, 2019, 9:03pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/2 "2019-07-30T21:03:07Z")

</div>

With your array of substrings, can you do

```julia
DataFrame(vcat(A...))

```

?

---

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [July 30, 2019, 9:19pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/3 "2019-07-30T21:19:07Z")

</div>

This seems to create a dataframe with columns for “offset” and “ncodeunits” in addition to the source string.

---

<div class="post-metadata">

### Author: ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)
#### Post date: [July 30, 2019, 9:26pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/4 "2019-07-30T21:26:21Z")

</div>

I’m not entirely sure, what is the requested layout of the dataframe?  
The vector of substring arrays looks like [[“abc”, “xyz”],[“ghi”, “klm”],…]  
And you want to have a DataFrame like that:

header: [First, Second]  
row1: [“abc”, “xyz”]  
row2: [“ghi”, “klm”]  
…

Is that correct? Especially, do you have some code/minimum working example?

---

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [July 30, 2019, 9:32pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/5 "2019-07-30T21:32:27Z")

</div>

Yep. Maybe I should be iterating over the rows and just doing it all at once, i.e. putting the elements of the split string into some union tuple with the other data from the file and making the dataframe out of those, somehow?

---

<div class="post-metadata">

### Author: ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)
#### Post date: [July 30, 2019, 9:34pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/6 "2019-07-30T21:34:55Z")

</div>

As a first “just works” idea (probably with poor performance), you can try to put `zip(arr...)` into the DataFrame rather than just `arr`. That should somewhat flip it over.  
(Here `arr` is the vector of substring arrays)

---

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [July 30, 2019, 9:45pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/7 "2019-07-30T21:45:35Z")

</div>

Sorry I can’t put more input/output up right now, I’m phone posting from my desk. I’ll try to replicate this truly amazing error later with dummy inputs… but yes, the zip method completely breaks Julia, at least on Windows…

---

<div class="post-metadata">

### Author: ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)
#### Post date: [July 30, 2019, 9:50pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/8 "2019-07-30T21:50:04Z")

</div>

Reason why you have multiple columns rather than rows in your dataframe is because the dataframe constructor accepts its inputs column by column (AFAIK for type stability reasons). Using `zip` here was the attempt to merge your data into three columns. Another way to build the DataFrame would be to do it incrementally. For that I recommend looking here:

> [@Adding a new row to a DataFrame](https://discourse.julialang.org/t/adding-a-new-row-to-a-dataframe/1331/4):
>
> Another solution using push!() : julia\> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"]) 4×2 DataFrames.DataFrame │ Row │ A │ B │ ├─────┼───┼─────┤ │ 1 │ 1 │ "M" │ │ 2 │ 2 │ "F" │ │ 3 │ 3 │ "F" │ │ 4 │ 4 │ "M" │ julia\> push!(df,[6 "F"]) julia\> df 5×2 DataFrames.DataFrame │ Row │ A │ B │ ├─────┼───┼─────┤ │ 1 │ 1 │ "M" │ │ 2 │ 2 │ "F" │ │ 3 │ 3 │ "F" │ │ 4 │ 4 │ "M" │ │ 5 │ 6 │ "F" │

and for size hints to the DataFrame, there:

> <https://stackoverflow.com/questions/28666466/preallocate-a-data-frame-of-known-size-in-julia>

---

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [July 30, 2019, 9:51pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/9 "2019-07-30T21:51:54Z")

</div>

Thanks, I’ll keep reading!

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [July 31, 2019, 8:13pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/10 "2019-07-31T20:13:57Z")

</div>

How about this

```julia
julia> using DataFrames, Random

julia> strings = ["$(randstring(3)).$(randstring(3))" for _ in 1:10]
10-element Array{String,1}:
 "ooJ.qvz"
 "jnk.pGB"
 "LcH.FID"
 "Yh0.ipI"
 "Cm0.aAd"
 "YA6.bNs"
 "kLF.dTe"
 "RTe.7cE"
 "dw4.j7S"
 "3vh.XC6"

julia> df = DataFrame([(a=a,b=b) for (a,b) in split.(strings, ".")])
10×2 DataFrame
│ Row │ a │ b │
│ │ SubStrin… │ SubStrin… │
|-----|-----------|-----------|
│ 1 │ ooJ │ qvz │
│ 2 │ jnk │ pGB │
│ 3 │ LcH │ FID │
│ 4 │ Yh0 │ ipI │
│ 5 │ Cm0 │ aAd │
│ 6 │ YA6 │ bNs │
│ 7 │ kLF │ dTe │
│ 8 │ RTe │ 7cE │
│ 9 │ dw4 │ j7S │
│ 10 │ 3vh │ XC6 │

```

This works because the `DataFrame()` constructor can take an array of named tuples

---

<div class="post-metadata">

### Author: ![FredC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredc/32/9857_2.png) [@FredC](https://discourse.julialang.org/u/FredC)
#### Post date: [August 1, 2019, 4:25pm UTC](https://discourse.julialang.org/t/split-string-to-multiple-columns/26994/11 "2019-08-01T16:25:32Z")

</div>

That’s the ticket, thanks!
