# Extra several columns from a dataframe to create a new dataframe in julia

**URL:** <https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049>\
**Category:** General Usage\
**Created:** [April 11, 2019, 7:04pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049 "2019-04-11T19:04:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 11, 2019, 7:04pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/1 "2019-04-11T19:04:31Z")

</div>

This is a simple task. Suppose I have a dataframe with name x. And I’d like to extract the columns with name x.name1, x.name3, x.name4, and combine them to create a new data frame using Julia.  
I tried the following and got an error message.  
`newDF = DataFrame[newDF.name1=x.name1, newDF.name2=x.name3, newDF.name3=x.name4]`  
Can anyone let me know how to fix this?  
Moreover, is this a fairly efficient way to handle large CSV files? Because I got data frame x from a csv file, and eventually, I need to write this newDF into a csv file.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [April 11, 2019, 7:36pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/2 "2019-04-11T19:36:17Z")

</div>

```julia
newDF = x[[:name1,:name3,:name4]]

```

will do the trick.

DataFramesMeta also has some helper functions like `@select` that would help with this in more complex scenarios.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [April 11, 2019, 7:48pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/3 "2019-04-11T19:48:03Z")

</div>

Also worth noting that the reasons you got an error: the constructor for `DataFrame` needs to be called with parentheses, not square brackets. And once you change the brackets, `DataFrame` tries to parse `newDF.name1` etc. as `Symbol`s to use as column names, but can’t figure them out because they contain dots.

Efficiency-wise, as long as the whole data frame fits in memory, yes, it is fairly efficient.

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 6:16am UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/4 "2019-04-12T06:16:38Z")

</div>

Thank you for your reply. But in the new data frame `newDF`, I’d like the column names to be different than simply `name1`, `name3` and `name4`. I can create the dataframe first and then change it later on. Was wondering, is it possible to change it directly from the very beginning of the construction of the new data frame?

---

<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:** [April 12, 2019, 12:35pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/5 "2019-04-12T12:35:35Z")

</div>

> [@bsnyh](#):
>
> Was wondering, is it possible to change it directly from the very beginning of the construction of the new data frame?

Yes, your code works if you follow @ElOceanografo’s suggestion and change the square brackets to parentheses in the constructor and don’t use periods in your column names. Or if you _really_ want periods, do for example `Symbol("df1.name1")`

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [April 12, 2019, 2:55pm UTC](https://discourse.julialang.org/t/extra-several-columns-from-a-dataframe-to-create-a-new-dataframe-in-julia/23049/6 "2019-04-12T14:55:27Z")

</div>

Thank you!
