Creating network with networkx / plotly from a pandas dataframe

I'd trying to build a network from a pandas dataframe with four columns that look like this:

PTDID | PBDID | BTDID | BDDID

PTD000000 | PBD000024 | BTD000264 | BDD000288

PTD000001 | PBD000025 | BTD000264 | BDD000289

PTD000001 | PBD000025 | BTD000264 | BDD000290

PTD000001 | PBD000025 | BTD000264 | BDD000291

PTD000001 | PBD000025 | BTD000264 | BDD000292

PTD000000 | PBD000024 | BTD000264 | BDD000293

...and so on for 36k rows

I'm trying to recreate this example with my dataframe : https://plot.ly/python/network-graphs/

However, I'm getting a 'KeyError: 0' message.

my current code:

import pandas as pd
import plotly.plotly as py
from plotly.graph_objs import *
import networkx as nx
import numpy as np


df=pd.read_csv('C:UsersnkurdobDocumentsbdh5.csv')

#get node positions

G=nx.from_pandas_dataframe(df, 'BTDID', 'BDDID', ['PTDID', 'PBDID'])
pos=nx.get_node_attributes(G,'pos')


dmin=1
ncenter=0
for n in pos:
    x,y=pos[n]
    d=(x-0.5)**2+(y-0.5)**2
    if d<dmin:
        ncenter=n
        dmin=d

p=nx.single_source_shortest_path_length(G,ncenter)

Does anyone know how to get past this error? I think this line might be messing it up:

G=nx.from_pandas_dataframe(df, 'BTDID', 'BDDID', ['PTDID', 'PBDID'])

but I'm not sure how to fix it. I got this ^ by following the example shown here: http://networkx.readthedocs.io/en/latest/reference/generated/networkx.convert_matrix.from_pandas_dataframe.html

Thank you in advance!

Nazar

Edit: Traceback error:

runfile('L:/Data/Global ID/Nazar/bdh.py', wdir='L:/Data/Global ID/Nazar') Traceback (most recent call last):

File "", line 1, in runfile('L:/Data/Global ID/Nazar/bdh.py', wdir='L:/Data/Global ID/Nazar')

File "C:UsersnkurdobAppDataLocalContinuumAnaconda2libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 714, in runfile execfile(filename, namespace)

File "C:UsersnkurdobAppDataLocalContinuumAnaconda2libsite-packagesspyderlibwidgetsexternalshellsitecustomize.py", line 74, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "L:/Data/Global ID/Nazar/bdh.py", line 32, in p=nx.single_source_shortest_path_length(G,ncenter)

File "C:UsersnkurdobAppDataLocalContinuumAnaconda2libsite-packagesnetworkxalgorithmsshortest_pathsunweighted.py", line 63, in single_source_shortest_path_length nextlevel.update(G[v]) # add neighbors of v

File "C:UsersnkurdobAppDataLocalContinuumAnaconda2libsite-packagesnetworkxclassesgraph.py", line 407, in getitem return self.adj[n]

KeyError: 0


It does not look like you have defined pos as a node attribute. So when you do pos = nx.get_node_attributes(G,'pos') , it is made into an empty dict .

Hence the loop for n in pos does nothing. So ncenter is still 0 after the loop. Since your graph doesn't have a node 0 , you're getting a keyerror when you ask it to find the paths from ncenter .

链接地址: http://www.djcxy.com/p/55290.html

上一篇: Python在map中执行subprocess.call

下一篇: 使用networkx / plotly从熊猫数据框创建网络