However, in most cases, you will have a CSV file with named columns. In that case, it is much more convenient to use a column header as a reference instead of numbers. This is possible with Cypher by specifying the WITH HEADERS option in the LOAD CSV query:
LOAD CSV WITH HEADERS FROM '<path/to/file.csv>' AS row
CREATE (:Node {name: row.name})
Let's practice with an example. The usa_state_neighbors_edges.csv CSV file has the following structure:
code;neighbor_code
NE;SD
NE;WY
NM;TX
...
This can be explained as follows:
- code is the two-letter state identifier (for example, CO for Colorado).
- neighbor_code is the two-letter identifier of a state sharing a border with the current state.
Our goal is to create a graph where each state is a node, and we create a relationship between two states if they share a common border.
So, let's get started:
- Fields in this CSV file are delimited with semi-colons, ;, so we have to use the FIELDTERMINATOR...