Get the columns name of a .csv with LOAD CSV in the right order

Hello everyone,

I wanted to load a .csv with LOAD CSV and get the columns name in that file using :
load csv with headers from 'file:///test.csv' as row with row limit 1 return keys(row);

The result is the columns name but in alphabetical order
The problem is that I need them in the same order as in the csv file. Is there a way to do so ?

Hello @leo.wehrli and welcome to the Neo4j community :slight_smile:

That's weird, it should not do this, which version of Neo4j are you using?

Regards,
Cobra

From call dbms.components() yield name, versions, edition unwind versions as version return name, version, edition;

"Neo4j Kernel" "4.2.1" "enterprise"

Can you share your CSV file and the result of your query?

Sorry for the lateness, I tried to upgrade neo4j to see if it came from there but it seems it isn't

For example, my csv is this :

name,a,b,c,d,e
o1,0,1,1,0,1
o2,1,0,1,1,0
o3,1,1,1,1,0
o4,1,0,0,1,0
o5,1,1,1,1,0
o6,1,0,1,1,0 

and my command is LOAD CSV WITH HEADERS FROM "file:///data1.csv" AS row return keys(row)

The result is then ["a", "b", "c", "d", "e", "name"] 6 times

It puts name at the end because of the alphabetical order instead of the first column like in the csv

I got the same result so I found a workaround:

LOAD CSV FROM "file:///data1.csv" AS row
RETURN row
LIMIT 1

The result is ["name", "a", "b", "c", "d", "e"] :slight_smile:

That's... weird, but hey, if that works

Edit : oh, I didn't see you removed the header, I thought you just added the limit 1

Rows with headers return map/dict/hash objects where the original order is not kept.

Cobras tip is the best then you get a list which is in order.

If you want to you can then use a second call to load csv with headers and use the colums from that list with row[column]

Thanks for the tips, I achieved to do what I wanted with that