Search code examples
python-3.xpandasnetworkx

How to convert each row in data frame to a node with attributes?


Given a sample data frame df:

import pandas as pd

df = pd.DataFrame({
    'id': [1, 2, 3, 4, 5],
    'a': [55, 2123, -19.3, 9, -8], 
    'b': ['aa', 'bb', 'ad', 'kuku', 'lulu']
})

Now I want to "upload" this data to a graph. Every row should be a node with id, a, and b attributes.

I have tried to do this with from_pandas_dataframe NetworkX method.

Please advise which function is responsible to do this in NetworkX?


Solution

  • The from_pandas_edgelist function requires both the source and target.

    from networkx import nx
    G = nx.from_pandas_edgelist(df, source='id', target='col_target')
    

    You could use a loop to generate the graph:

    from networkx import nx
    G = nx.Graph()
    
    for i, attr in df.set_index('id').iterrows():
        G.add_node(i, **attr.to_dict())
    

    Or, with from_pandas_edgelist, if you really want only the nodes, you might want to add a dummy target:

    G = nx.from_pandas_edgelist(df.assign(target='none'),
                                source='id', target='target',
                                edge_attr=['a', 'b'])
    

    enter image description here

    Then remove the dummy node:

    G.remove_node('none')
    

    enter image description here