Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia DataFrame columns starting with number?

Tags:

julia

This may be a stupid question, but for the life of me I can't figure out how to get Julia to read a csv file with column names that start with numbers and use them in DataFrames. How does one do this?

For example, say I have the file "test.csv" which contains the following:

,1Y,2Y,3Y
1Y,11,12,13
2Y,21,22,23

If I just use readtable(), I get this:

julia> using DataFrames

julia> df = readtable("test.csv")
2x4 DataFrames.DataFrame
| Row | x    | x1Y | x2Y | x3Y |
|-----|------|-----|-----|-----|
| 1   | "1Y" | 11  | 12  | 13  |
| 2   | "2Y" | 21  | 22  | 23  |

What gives? How can I get the column names to be what they're supposed to be, "1Y, "2Y, etc.?

like image 893
Robert Mah Avatar asked Feb 08 '23 00:02

Robert Mah


1 Answers

The problem is that in DataFrames, column names are symbols, which aren't meant to (see comment below) start with a number.

You can see this by doing e.g. typeof(:2), which will return Int64, rather than (as you might expect) Symbol. Thus, to get your columnnames into a useable format, DataFrames will have to prefix it with a letter - typeof(:x2) will return Symbol, and is therefore a valid column name.

like image 89
Nils Gudat Avatar answered Feb 16 '23 19:02

Nils Gudat