0

I am using python to draw an edge graph. Right now my data look like this

enter image description here

I have two columns of integer. Now, I want to make them into a (x,y) object to use as a node. For example, row 2 has two values 38 and 956; I want to create a new object from it called node that will be a two-tuple node like (38,956). My aim is to use it with the A* algorithm.

So, how do I turn two integers into a node of two values?

Anh Duy
  • 23
  • 3
  • 1
    Use the `csv` module to read your file, and you'll get a list of 2-item lists, which is pretty much the data structure you need. https://docs.python.org/3/library/csv.html – slothrop Apr 21 '23 at 15:01
  • It does, Pranav. However, now it reads in the index. Is there anyway to remove the index? – Anh Duy Apr 21 '23 at 15:21

1 Answers1

2

With , you can use read_csv, agg & to_list :

#pip install pandas
import pandas as pd

nodes = pd.read_csv("person_knows_person_cleaned.csv").agg(tuple, axis=1).tolist()

Output :

print(nodes)

[(38, 956),
 (38, 962),
 (38, 941),
 (38, 74),
 (38, 36),
 (38, 53),
 (38, 48),
 (38, 29),
 (38, 46),
 (38, 40),
 (38, 60),
 (38, 31),
 (38, 41),
 (38, 6),
 (38, 4),
 (38, 547),
 (38, 832)]
Timeless
  • 22,580
  • 4
  • 12
  • 30