Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
06-25-2021 05:03 AM
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 ?
Solved! Go to Solution.
06-25-2021 07:08 AM
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"]
06-25-2021 05:09 AM
Hello @leo.wehrli and welcome to the Neo4j community
That's weird, it should not do this, which version of Neo4j are you using?
Regards,
Cobra
06-25-2021 05:17 AM
From call dbms.components() yield name, versions, edition unwind versions as version return name, version, edition;
"Neo4j Kernel" "4.2.1" "enterprise"
06-25-2021 05:56 AM
Can you share your CSV file and the result of your query?
06-25-2021 06:47 AM
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
06-25-2021 07:08 AM
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"]
06-25-2021 07:10 AM
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
06-25-2021 11:55 PM
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]
07-01-2021 08:02 AM
Thanks for the tips, I achieved to do what I wanted with that
All the sessions of the conference are now available online