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

Time:02-23

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?

CodePudding user response:

The enter image description here

Then remove the dummy node:

G.remove_node('none')

enter image description here

  • Related