# How to add a unit row to a CSV file through dataframe?

**URL:** https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241
**Category:** General Usage
**Tags:** question, dataframes, csv
**Created:** [October 22, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241 "2021-10-22T21:05:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 22, 2021, 9:05pm UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/1 "2021-10-22T21:05:50Z")

</div>

I need to write my matrix into a CSV file, due to the large size (~200 MB), it will be very hard to open it using Excel later on and manually add a 2nd row for the unit information.

Is there a better way to add such a row within Julia using Dataframe? Thanks!

The end CSV should look like this:

```julia
Longitude, Latitude, Depth, Oxygen
degrees, degrees, meters, mole/L
-100,15, 25, 300,
...

```

This is my current code:

```julia

df = DataFrame(
Longitude = TT[:,1],
Latitude = TT[:,2],
Depth = TT[:,3],
Oxygen = TT[:,4]
);

F1 = "sample.csv";
CSV.write(F1, df);

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [October 22, 2021, 9:08pm UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/2 "2021-10-22T21:08:57Z")

</div>

There is an `append = true` keyword argument to `CSV.write` which does this.

You can also add a row to a data frame with `push!` see `? push!` for more details (and scroll to the part about data frames).

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 22, 2021, 9:34pm UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/3 "2021-10-22T21:34:03Z")

</div>

> [@pdeffebach](#):
>
> `append = true`

Many thanks! I’m still having a hard time envisioning how to add a row after the header line and before the numerical values by using append = true. I wonder if you could share more details about this?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [October 22, 2021, 9:40pm UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/4 "2021-10-22T21:40:58Z")

</div>

I don’t believe you can add things to the top of the CSV file like that. You can only append them to the bottom of the file.

---

<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: [October 23, 2021, 6:56am UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/6 "2021-10-23T06:56:54Z")

</div>

One suggestion:

```julia
using CSV, DataFrames
header = ["Longitude", "Latitude", "Depth", "Oxygen"]
row2 = ["degrees" "degrees" "meters" "mole/L"]
TT = [-100 15 25 300; -90 35 10 250]
file1 = "df_2header_rows.csv"
CSV.write(file1, DataFrame([]); header=header)
CSV.write(file1, DataFrame(row2,:auto); append=true)
CSV.write(file1, DataFrame(TT,:auto); append=true)

```

It produces \*.csv file:

```julia
Longitude,Latitude,Depth,Oxygen
degrees,degrees,meters,mole/L
-100,15,25,300
-90,35,10,250

```

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 24, 2021, 11:40am UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/8 "2021-10-24T11:40:42Z")

</div>

Many thanks again for the suggested solution!

In reality, my TT contains string columns as well. Is it possible to rely on my dataframe, instead of using the numerical array TT in this step?  
`CSV.write(F4, DataFrame(TT,:auto); append=true);`

Basically do something like the below?

```julia
df = DataFrame(
Station = TT[:,1],
Latitude = TT[:,2],
Depth = TT[:,3],
Oxygen = TT[:,4]
);

CSV.write(F4, DataFrame(df[2:end,:],:auto); append=true);

```

Right now, it gives me this error:

```julia
LoadError: MethodError: no method matching DataFrame(::DataFrame, ::Symbol)
Closest candidates are:
  DataFrame(::AbstractVector{T} where T, ::Symbol; copycols)

```

Or is there a better way to create an TT with some columns being strings?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 24, 2021, 11:41am UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/9 "2021-10-24T11:41:54Z")

</div>

> [@leon](#):
>
> `DataFrame(df[2:end,:],:auto);`

what are you trying to achieve with this? isn’t `df[2:end,:]` already a DataFrame?

---

<div class="post-metadata">

### Author: ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)
#### Post date: [October 24, 2021, 11:49am UTC](https://discourse.julialang.org/t/how-to-add-a-unit-row-to-a-csv-file-through-dataframe/70241/10 "2021-10-24T11:49:12Z")

</div>

Many thanks! That was the problem.

I have changed it to the below and it is now working!  
`CSV.write(F4, df[2:end,:]; append=true);`
