python - Make NetworkX node attributes into Pandas Dataframe columns -


i have networkx graph called g created below:

import networkx nx g = nx.graph() g.add_node(1,job= 'teacher', boss = 'dee') g.add_node(2,job= 'teacher', boss = 'foo') g.add_node(3,job= 'admin', boss = 'dee') g.add_node(4,job= 'admin', boss = 'lopez') 

i store node number along attributes, job , boss in separate columns of pandas dataframe.

i have attempted below code produces dataframe 2 columns, 1 node number , 1 of attributes:

graph = g.nodes(data = true) import pandas pd df = pd.dataframe(graph)  df out[19]:      0                                      1 0  1  {u'job': u'teacher', u'boss': u'dee'} 1  2  {u'job': u'teacher', u'boss': u'foo'} 2  3    {u'job': u'admin', u'boss': u'dee'} 3  4  {u'job': u'admin', u'boss': u'lopez'} 

note: acknowledge networkx has to_pandas_dataframe function not provide dataframe output looking for.

i don't know how representative data should straightforward modify code work on real network:

in [32]: data={} data['node']=[x[0] x in graph] data['boss'] = [x[1]['boss'] x in graph] data['job'] = [x[1]['job'] x in graph] df1 = pd.dataframe(data) df1  out[32]:     boss      job  node 0    dee  teacher     1 1    foo  teacher     2 2    dee    admin     3 3  lopez    admin     4 

so here i'm doing constructing dict graph data, pandas accepts dicts data keys column names , data has array-like, in case lists of values

a more dynamic method:

in [42]: def func(graph):     data={}     data['node']=[x[0] x in graph]     other_cols = graph[0][1].keys()     key in other_cols:         data[key] = [x[1][key] x in graph]     return data pd.dataframe(func(graph))  out[42]:     boss      job  node 0    dee  teacher     1 1    foo  teacher     2 2    dee    admin     3 3  lopez    admin     4 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -