3

I have created a weighted edge list that I am trying to use to generate a weighted undirected graph:

The data is in a csv which looks like the following in excel:

node1 node2 weight

a     b     0.1

a     c     0.3

As recommended by other StackOverflow posts, I have been using the following code to read in the csv:

fh=open("<file_location>.csv", 'r')
G = nx.read_weighted_edgelist(fh,delimiter=',')

The first line runs fine but the second yields the error message:

TypeError: Failed to convert weight data weight to type type 'float'

If I check G, it has read in the nodes fine but not the weights, any ideas?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
codegurl
  • 33
  • 1
  • 3
  • Looks like the problem is with file format vs what `read_weighted_edgelist` expects. So, 1)show the file as it is, not how it "looks in Excel"; 2) _"If I check G, it has read in the nodes fine but not the weights"_ - show here what tells you so. – ivan_pozdeev Apr 16 '17 at 19:07
  • Hi Ivan, thanks for your comment, I'm new to this so any help is much appreciated. 1)The data is in a csv with three columns, is there a way that I can look at it as it is without it opening in excel? 2) If I call nx.info(G) it returns 'Name: \nType: Graph\nNumber of nodes: 53\nNumber of edges: 1378\nAverage degree: 52.0000' – codegurl Apr 16 '17 at 19:15
  • 1) open it with anything other than Excel, duh! Google that. – ivan_pozdeev Apr 16 '17 at 19:21
  • Sorry just got there: a_node1,b_node2,c_weight firstname_secondname,firstname_secondnames,0.103883495146 firstname_secondname,firstname_secondname,0.105935386927 firstname_secondname,firstname_secondname,0.134917267713 – codegurl Apr 16 '17 at 19:24
  • As [read_weighted_edgelist — NetworkX 1.10 documentation](https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.readwrite.edgelist.read_weighted_edgelist.html) says, the format it expects is not CSV but delimiter-separated data, with whitespace as the default delimiter. If you have another delimiter, pass it as the corresponding argument. – ivan_pozdeev Apr 16 '17 at 19:26
  • Hi Ivan, sorry I'm missing something here, can you explain why my data is not in the right format? – codegurl Apr 16 '17 at 19:29

2 Answers2

4

EDIT: restructured to include explanation for why code fails and how to resolve, following @Joel's suggestion. @Joel's answer provides an explanation of why the code fails, but not a suggestion for how to move resolve it.

Solution

nx.read_weighted_edgelist will ignore input lines that start with #, so if you change the first line of your csv file from

node1 node2 weight

to

#node1 node2 weight

then you should be able to read in the network with weights.

Also note that read_weighted_edgelist accepts a file path (string) as well as a file handle, so if you aren't using fh again, you don't need to open it first, just pass it directly.

G = nx.read_weighted_edgelist("<file_location>.csv", delimiter=',')

Why your code failed

(this is incorporated from the answer of @Joel)

When it encounters the first line (from your comments: a_node1,b_node2,c_weight )

it interprets the first node to be a_node1, the second to be b_node2, and it tries to assign the weight c_weight to the edge between them.

It is difficult to convert the string c_weight to a float. So it gives an error.

Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • glad it helped! - if this (or other solutions solve your problem, you can mark it as accepted with the green tick. – Bonlenfum Apr 18 '17 at 12:01
  • @Bonlenfum - could you edit my response into your answer? It would be better to have a single complete answer than two partial answers. – Joel Apr 18 '17 at 13:51
1

When it encounters the first line (from your comments: a_node1,b_node2,c_weight )

it interprets the first node to be a_node1, the second to be b_node2, and it tries to assign the weight c_weight to the edge between them.

It is difficult to convert the string c_weight to a float. So it gives an error.

Joel
  • 22,598
  • 6
  • 69
  • 93