# Extract specific subgraph and create appropriate .csv files to re-import it in a new db

**URL:** <https://community.neo4j.com/t/extract-specific-subgraph-and-create-appropriate-csv-files-to-re-import-it-in-a-new-db/30800>\
**Category:** Cypher\
**Created:** [December 22, 2020, 10:19am UTC](https://community.neo4j.com/t/extract-specific-subgraph-and-create-appropriate-csv-files-to-re-import-it-in-a-new-db/30800 "2020-12-22T10:19:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![and\_manousakis](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/and_manousakis/32/11628_2.png) [@and\_manousakis](https://community.neo4j.com/u/and_manousakis)\
**Post date:** [December 22, 2020, 10:19am UTC](https://community.neo4j.com/t/extract-specific-subgraph-and-create-appropriate-csv-files-to-re-import-it-in-a-new-db/30800/1 "2020-12-22T10:19:54Z")

</div>

I have a set of nodes extracted from a network bitcoin transaction network using the following query:

```auto
MATCH (root:Address {AddressID: $val})-[r*..k]->(target:Transaction)
RETURN DISTINCT(target.Hash) AS Targets

```

where k = 1, 3 and 5 (i.e. hops away from the root node). The result is a set of nodes (~210000) in a .csv file with two columns: [k, TxHash]

I would like to create the relationship files between the extracted nodes. The easiest way would be to create a nested for loop in the following manner:

```auto
q1 = """
	MATCH (src:Transaction {Hash: $val1})-[op:OutgoingPayment]->(a:Address)-[ip:IncomingPayment]->(dst:Transaction {Hash: $val2})
	RETURN DISTINCT(dst.Hash) AS Target
	"""

for src in neighbors:
	for dst in neighbors:
		output = graph.run(q1, parameters = {"val1": str(src), "val2": str(dst)}).to_data_frame()

```

Even though it's the easiest way, it will also take a lot of time. Is there some other more efficient way to achieve this?

Thank you!

P.S. The overall goal is starting from a seed node to visit its neighbors for k = 1, 2, ..., n and create the appropriate .csv files for bulk import in neo4j. In other words, identify a specific subgraph, create the appropriate .csv files and re-import it in a new neo4j db.
