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.
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.
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.
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.
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.
Here's a one-liner.
pd.DataFrame.from_dict(dict(graph.nodes(data=True)), orient='index')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With