"Elongate" data frame based on column that contains arrays

Hi everyone,

Im looking for a function that surely exists, but I just cant find it.
I want to take a data frame, and create additional rows based on one of its columns which has arrays as entries. The idea is to split up the arrays of said column.

Example:

df = DataFrame(a = [1, 2, 3, [4, 4], 5], b = ["a", "b", "c", "e", "f"], c = [[22, 22], [33, 33], 44, 55, [66, 66, 66]])

function(df, :c)

should output

df = DataFrame(a = [1, 1, 2, 2, 3, [4, 4], 5, 5, 5], b = ["a", "a", "b", "b", "c", "e", "f", , "f", , "f"], c = [22, 22, 33, 33, 44, 55, 66, 66, 66])

I specified in the function I should elongate the df by :c, so the [4, 4] array in :a should remain untouched and, in case it’d be in row 1, should also get duplicated (which wouldn’t be the case here.

Is anyone aware of something like that?

Thanks a lot!

Try flattening it:

flatten(df, :c)
4 Likes

Exactly what I was looking for, thank you!