# DataFrame with "any" vector

**URL:** <https://discourse.julialang.org/t/dataframe-with-any-vector/94688>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [February 15, 2023, 3:21pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688 "2023-02-15T15:21:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MitchellWeg](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MitchellWeg](https://discourse.julialang.org/u/MitchellWeg)\
**Post date:** [February 15, 2023, 3:21pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/1 "2023-02-15T15:21:19Z")

</div>

I want to dynamically create a DataFrame. This means that I don’t know the column names and values beforehand.

`Any[1, "\"I am foo\"", 2, "\"You are bar\"", 3, "\"We are foobar\""]`

And to create the DataFrame, I do:

`df = DataFrame(new_data, column_names)`

Where column\_names is:

`["id", "foo"]`.

However, I’m getting the error:

`ArgumentError: columns argument must be a vector of AbstractVector objects`

How can I do this better?

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [February 15, 2023, 3:33pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/2 "2023-02-15T15:33:17Z")

</div>

Just a first question to understanding, is your first line of code, i.e.

```plaintext
A = Any[1, "\"I am foo\"", 2, "\"You are bar\"", 3, "\"We are foobar\""]

```

meant to. be your “foo” column? Then I think you are missing an Id column.

If you take

```julia
B = [1,2,3,4,5,6]

```

then

```julia
julia> df = DataFrame([A,B], ["id", "foo"])
6×2 DataFrame
 Row │ id foo
     │ Any Any
─────┼──────────────────────
   1 │ 1 1
   2 │ "I am foo" 2
   3 │ 2 3
   4 │ "You are bar" 4
   5 │ 3 5
   6 │ "We are foobar" 6

```

Ah, or maybe you have 3 data sets in there? Then they need to be separate vectors, i.e.

```julia
df2 = DataFrame([[1,2,3], ["Foo", "Bar", "Baz"] ], ["id","foo"])

```

also works perfectly fine – the problem might be that you have your data as _one_ vector instead of 2 here.

---

<div class="post-metadata">

**Author:** ![MitchellWeg](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MitchellWeg](https://discourse.julialang.org/u/MitchellWeg)\
**Post date:** [February 15, 2023, 3:36pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/3 "2023-02-15T15:36:08Z")

</div>

My first line is the complete dataset, the integers are the ID’s, the strings is the ‘foo’ column. If the complete dataset are all strings, it does work.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [February 15, 2023, 3:41pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/4 "2023-02-15T15:41:40Z")

</div>

From the error message (that says you need a vector of vectors) you have to have your data in the form I wrote in the second example then, I think.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [February 15, 2023, 3:47pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/5 "2023-02-15T15:47:25Z")

</div>

Or if your dataset from above is stored in `A`, then you could also do

```julia
DataFrame([A[1:2:end], A[2:2:end]], ["Id", "Foo"])

```

as long as you know how many columns you have – if you do not know that, it might be complicated/impossible, since how should one obtain that information from the vector.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 15, 2023, 11:35pm UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/6 "2023-02-15T23:35:09Z")

</div>

For a slightly more general case, you could use `permutedims(reshape())`:

```julia
using DataFrames
vdata = Any[1, "\"I am foo\"", 2, "\"You are bar\"", 3, "\"We are foobar\""]
colnames = ["id", "foo"]
mdata = permutedims(reshape(vdata, length(colnames), :))
df = DataFrame(mdata, colnames)

```

---

<div class="post-metadata">

**Author:** ![MitchellWeg](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MitchellWeg](https://discourse.julialang.org/u/MitchellWeg)\
**Post date:** [February 16, 2023, 10:32am UTC](https://discourse.julialang.org/t/dataframe-with-any-vector/94688/7 "2023-02-16T10:32:05Z")

</div>

Thanks, that works!
