Removing the header of a dataframe

Hi all,

I have read in a csv using “csv.read()” and then using the link below I have transposed the data.
Transpose of Julia DataFrame - Stack Overflow

This is great and transposes the data as expected, however Julia has added a header on top of the header that already existed.
When I return println(names(df)) I get:
[“variable”, “1”, “2”, “3”, “4”]

Whereas I would expect a header of:
[“YEAR”, “CASH”, “COUNTRY”, “REGION”, “INDICATOR”]
Which is now in Row2

However I want to use the actual header names which are now on row 2, is there any way I can remove this sudo-header and use the actual headers?

My next step is to produce a new column which will sum and subtract based on the column headers “CASH”, “COUNTRY”, “REGION”, “INDICATOR”

I can remove the headers using csv.write(headers = false). However, if it were possible to do this without outputting the data then that would be ideal.

Any suggestions would be greatly appreciated!

julia> DataFrame(x[2:end, :], string.(x[1, :]))
1×2 DataFrame
 Row │ x    y   
     │ Any  Any 
─────┼──────────
   1 │ 1    2

julia> using DataFrames

julia> df = DataFrame(a = ["x", 1], b = ["y", 2])
2×2 DataFrame
 Row │ a    b   
     │ Any  Any 
─────┼──────────
   1 │ x    y
   2 │ 1    2

julia> x = Matrix(df)
2×2 Matrix{Any}:
  "x"   "y"
 1     2

julia> DataFrame(x[2:end, :], string.(x[1, :]))
1×2 DataFrame
 Row │ x    y   
     │ Any  Any 
─────┼──────────
   1 │ 1    2

But also note that you can just read in the csv transposed using the transpose = true kwarg, from the CSV.File docstring:

• transpose::Bool: read a csv file "transposed", i.e. each column is parsed as a row
2 Likes

Thank you so much :slight_smile: didnt realise that was a built in function, thank you. will definitely use this again :smiley: