[PYTHON] Read the linked list in csv format with graph-tool

Thing you want to do

Consider reading a file in the following linked list (source, dest, weight) format with graph-tool.

link_list.csv


Alice,Bob,2
Bob,Charlie,5
Charlie,Alice,3

code

To read this file with grpah-tool, parse it using the csv module as follows.

load_csv_link_list.py


import csv
import graph_tool.all as gt

nodes = set()
rows = []

with open('link_list.csv') as f:
    reader = csv.reader(f)
    rows = [row for row in reader]

for row in rows:
    nodes.add(row[0])
    nodes.add(row[1])

nodes = list(nodes)
g = gt.Graph()
g.vp['name'] = g.new_vp('string')

for n in nodes:
    v = g.add_vertex()
    g.vp['name'][v] = n

# set edges
g.ep['weight'] = g.new_ep('double')

for row in rows:
    s = nodes.index(row[0])
    t = nodes.index(row[1])
    w = float(row[2])
    vs = g.vertex(s)     # => get vertex object from the index
    vt = g.vertex(t)
    e = g.add_edge(vs, vt)
    g.ep['weight'][e] = w

What we are doing is as follows

--First, read all the contents of the csv file and create a list of nodes nodes --I want to extract only unique things, so I store them in set and then convert them to list. --The node name creates a string vertex_property name in graph and storesg.vp ['name'] = g.new_vp ('string') --g.vp holds the attribute (vertex_property) for the node --Edge weights are stored by creating a floating point type edge_property weight

This is what the graph looks like.

result

By the way, the result of visualizing this graph is as follows.

image.png

pos = gt.arf_layout(g)
gt.graph_draw(g, pos=pos, vertex_text=g.vp.name, vertex_font_size=10)

Recommended Posts

Read the linked list in csv format with graph-tool
Read all csv files in the folder
Read the csv file and display it in the browser
Get the result in dict format with Python psycopg2
Methods available in the list
Read the URL list with Robot Framework and surround the screenshots
[Python] Read the csv file and display the figure with matplotlib
Format the CSV file of "National Holiday" of the Cabinet Office with pandas
The story of outputting the planetarium master in pdf format with Pycairo
Read a file in Python with a relative path from the program
Read files in parallel with Python
Format the Git log and get the committed file name in csv format
Read and format a csv file mixed with comma tabs with Python pandas
Generate a list packed with the number of days in the current month.
Receive a list of the results of parallel processing in Python with starmap
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Linked list (list_head / queue) in C language
Touch around the twitter list with tweepy
Behavior when returning in the with block
Put together consecutive values in the list
OR the List in Python (zip function)
Display Python 3 in the browser with MAMP
Change the list in a for statement
Let's read the RINEX file with Python ①
Read and write csv files with numpy
Read the file line by line in Python
I saved the scraped data in CSV!
Read the file line by line in Python
Escape curly braces in the format string
Export Amazon RDS tables in CSV format
Get the EDINET code list in Python
How to read CSV files in Pandas
[Python] Read the specified line in the file
Read text in images with python OCR
Read CSV and analyze with Pandas and Seaborn
Various ways to read the last line of a csv file in Python
linked list
Replace the named entity in the read text file with a label (using GiNZA)
How to get a list of files in the same directory with python
How to read a CSV file with Python 2/3
[Python] Get the files in a folder with Python
Load the network modeled with Rhinoceros in Python ③
Change the device (drive) label in vfat format.
Read JSON with Python and output as CSV
[Automation] Extract the table in PDF with Python
Read table data in PDF file with Python
Get only the subclass elements in a list
Read the output of subprocess.Popen in real time
Determine the numbers in the image taken with the webcam
Convert UTF-8 CSV files to read in Excel
Detect folders with the same image in ImageHash
Load the network modeled with Rhinoceros in Python ②
In print, the non-ascii characters in the list look great
Search by the value of the instance in the list
[Django] css in the project cannot be read
Load the network modeled with Rhinoceros in Python ①
The story that fits in with pip installation
Convert strings to character-by-character list format with python
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
[Python] Read Japanese csv with pandas without garbled characters (and extract columns written in Japanese)