Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make NetworkX node attributes into Pandas Dataframe columns

I have a Networkx graph called G created below:

import networkx as 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 would like to store the node number along with attributes, job and boss in separate columns of a pandas dataframe.

I have attempted to do this with the below code but it produces a dataframe with 2 columns, 1 with node number and one with all of the attributes:

graph = G.nodes(data = True)
import pandas as 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: I acknowledge that NetworkX has a to_pandas_dataframe function but it does not provide a dataframe with the output I am looking for.

like image 586
BeeGee Avatar asked Jan 27 '16 19:01

BeeGee


People also ask

How do I convert a column to a DataFrame in Pandas?

In Pandas, columns and dataframes can be transformed and manipulated using methods such as apply() and transform() . The desired transformations are passed in as arguments to the methods as functions. Each method has its subtle differences and utility.

How do you give columns to a DataFrame?

To assign new columns to a DataFrame, use the Pandas assign() method. The assign() returns the new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. The length of the newly assigned column must match the number of rows in the DataFrame.

Which attribute is used with DataFrame?

There are two types of index in a DataFrame one is the row index and the other is the column index. The index attribute is used to display the row labels of a data frame object.

What are the attributes of Pandas DataFrame?

A DataFrame has two types of indexes: One is the row index and another type is a set of column indexes. The DataFrame attribute index returns the row index and the attribute columns returns the column indexes. The actual values of the DataFrame stored as an ndarray are provided by the attribute values.


1 Answers

Here's a one-liner.

pd.DataFrame.from_dict(dict(graph.nodes(data=True)), orient='index')
like image 92
iamjli Avatar answered Sep 18 '22 02:09

iamjli