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

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:

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

This is my current code:

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

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

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).

1 Like

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?

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.

1 Like

One suggestion:

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:


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?

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:

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?

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

1 Like

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);