-1

My code below is supposed to print a graph/network using Networkx, Pandas and data from a CSV file. The code is (networkx3.py) -

import csv
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx

g = nx.Graph()
csv_dict = pd.read_csv('Book1.csv', index_col=[0])
csv_1 = csv_dict.values.tolist()

ini = 0
for row in csv_1:
    for i in row:
        if type(row[i]) is str:
            g.add_edge(ini, int(i), conn_prob=(float(row[i])))

    max_wg_ngs = sorted(g[ini].items(), key=lambda e: e[1]["conn_prob"], reverse=True)[:2]
    sarr = [str(a) for a in max_wg_ngs]
    print "Neighbours of Node %d are:" % ini       
    #print(max_wg_ngs)
    for item in sarr:
        print ''.join(str(item))[1:-1]

    ini += 1

pos = nx.spring_layout(g, scale=100.)
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_edges(g, pos)
nx.draw_networkx_labels(g, pos)
#plt.axis('off')
plt.show()  

The data in the CSV file is (Book1.csv) -

,1,2,3,4,5,6,7,8,9,10
1,0,0.257905291,0.775104118,0.239086843,0.002313744,0.416936603,0.194817214,0.163350301,0.252043807,0.251272559
2,0.346100279,0,0.438892758,0.598885794,0.002263231,0.406685237,0.523850975,0.257660167,0.206302228,0.161385794
3,0.753358102,0.222349243,0,0.407830809,0.001714776,0.507573592,0.169905687,0.139611318,0.187910832,0.326950557
4,0.185342928,0.571302688,0.51784403,0,0.003231018,0.295197533,0.216184462,0.153032751,0.216331326,0.317961522
5,0,0,0,0,0,0,0,0,0,0
6,0.478164621,0.418192795,0.646810223,0.410746629,0.002414973,0,0.609176897,0.203461461,0.157576977,0.636747837
7,0.24894327,0.522914349,0.33948832,0.316240267,0.002335929,0.639377086,0,0.410011123,0.540266963,0.587764182
8,0.234017887,0.320967208,0.285193773,0.258198079,0.003146737,0.224412057,0.411725737,0,0.487081815,0.469526333
9,0.302955306,0.080506624,0.261610132,0.22856311,0.001746979,0.014994905,0.63386228,0.486096957,0,0.664434415
10,0.232675407,0.121596312,0.457715027,0.310618067,0.001872929,0.57556548,0.473562887,0.32185564,0.482351246,0

The code however doesn't work. I don't understand where I'm going wrong. The error is -

Traceback (most recent call last):
  File "networkx3.py", line 13, in <module>
    if type(row[i]) is str:
TypeError: list indices must be integers, not float

I do not want to modify the CSV file or its data. The index column and header are supposed to be ignored.
I have previously asked this question but I did not get satisfactory answers. Can anybody help?
Thanks a lot in advance :) (Using Ubuntu 14.04 32-bit VM. Credits to @Adonis for helping in creating the original code)

user136819
  • 205
  • 7
  • 21
  • 1
    You have `for i in row`. The first time through that loop, what do you think the value of `i` is? Perhaps try printing it to be sure. The next question is why do you expect `row[i]` to be meaningful on the next line - what are you assuming the value of `i` is in that statement? Hint - the error message is telling you that the list index is not an integer. What is the only thing being used like a list index in that statement? – Joel Aug 18 '17 at 01:56
  • Thanks for replying @Joel . I am actually new to Python, hence I do not know a great deal. I did not understand the hint you gave earlier. But I tried printing `i` and they're the values are from the CSV file. – user136819 Aug 19 '17 at 14:42
  • So hopefully you've realized that `i` is what you expected `row[i]` to be. – Joel Aug 21 '17 at 15:46
  • @Joel that's right. But I'm still stuck with the code, unable to move forward :( . Any more suggestions you could give? I really appreciate your help – user136819 Aug 21 '17 at 21:22
  • so you shouldn't be doing anything with `row[i]`. Everywhere you have `row[i]` you almost certainly mean `i`. [and `i` is a poor choice for the name for the entry - you should use a more descriptive name] – Joel Aug 23 '17 at 02:58
  • @Joel I finally have the answer. Thanks again for the guidance :) – user136819 Aug 23 '17 at 11:39
  • Good. Please take some time to make sure you understand why what you were doing was an error. Otherwise, you'll end up repeating something similar in the future and it'll again take a while to get things fixed. Make sure you read about how for loops work in Python, for example read https://stackoverflow.com/a/4171389/2966723 – Joel Aug 23 '17 at 19:29

1 Answers1

0

A little late in answering my own question, but with some valuable help from @Joel and @Adonis, I finally figured out where I was going wrong.
The problem was in the 2nd for loop where I tried to pass a float value as a string into the Graph which gave me an error. Other minor changes would result in an output but without any edges, just nodes.
Finally, after using an enumerate function to define a connecting node (using its index giving power), I got the required output. The only changes to be made are in the 2nd for loop and the if condition:
for row in csv_1: for idx, i in enumerate(row): if type(row[idx]) is float: g.add_edge(ini, idx, conn_prob=(float(row[idx])))

Thanks to all the selfless guys at SOF for the help, couldn't have done it without you :)

user136819
  • 205
  • 7
  • 21