Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object is not subscripable networkx

import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
#--
edgelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew    /e570c38bcc72a8d102422f2af836513b/raw/89c76b2563dbc0e88384719a35cba0dfc04cd522/edgelist_sleeping_giant.csv')
edgelist.head(10)
#--
nodelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv')
nodelist.head(5)
#--
g = nx.Graph()
#--
for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
#--
#print(elrow[0])
#print(elrow[1])
#print(elrow[2:].to_dict())
#--
g.edges(data=True)[0:5]
g.nodes(data=True)[0:10]
#--
print(format(g.number_of_edges()))
print(format(g.number_of_nodes()))

Gets me the following error:

Traceback (most recent call last):
  File "C:/Users/####/Main.py", line 22, in <module>
    g.edges(data=True)[0:5]
TypeError: 'EdgeDataView' object is not subscriptable

I have read a couple of other threads but nada. From my simple understanding the error is caused by [0:5] but im most likely wrong.

I'm a fairly basic coder and am trying to follow this tutorial and i get the error above.

like image 625
Aarron Cooke Avatar asked Nov 16 '17 09:11

Aarron Cooke


1 Answers

The tutorial is based on a previous version of networkx where g.edges() or g.edges(Data=True) would give you a list of tuples. Lists are subscriptable.

The version you are running has a different output, g.edges gives you an EdgeView property, and g.edges(data=True) an EdgeDataView object which are not subscriptable. To answer your question, you can do:

list(g.edges(data=True))[0:5]

Note: the same is true for g.nodes(): before it was a list now it's a NodeView property not subscriptable. So don't forget to transform it to a list object before trying to add subscripts ([x:x]).

like image 199
rodgdor Avatar answered Oct 11 '22 12:10

rodgdor