Skip to content Skip to sidebar Skip to footer

Csv To Adjacency Matrix

I am trying to visualize some data I got using python and bokeh, but it is not working out. It is a .csv file and it looks like this, only then with much more rows and values: ;A;

Solution 1:

You probably want to convert it to an actual matrix, using numpy, for instance. Seems like the first list in your list of lists gives you the nodes for the columns, then the first value in each subsequent list is the node for each row. You could split each list by the ;, then select the values that aren't the node name (A,B, etc.) and store them as a row in your matrix, so that your matrix looks like

import numpy as np

# Numpy matrix, or array of a list of lists
np.array([[0,0,1,2],
          [0,3,0,0],
          [0,0,0,1],
          [1,0,2,0]])

Based on your comment, you could do something like

split_list = [row.split(';') forsublistin your_list forrowin sublist]

your_dict = {row[0]: row[1:] forrowin split_list[1:]}

#Output
{'A': ['0', '0', '1', '2'], 'B': ['0', '3', '0', '0']}

Post a Comment for "Csv To Adjacency Matrix"