Tuesday, 12 February 2019

NetworkX - Setting node attributes from dataframe

I'm having trouble figuring out how to add attributes to nodes in my network from columns in my dataframe.

I have provided an example of my dataframe below, there are around 10 columns in total, but I only use the 5 columns shown below when creating my network.

Unfortunately at the moment I can only get edge attributes working with my network, I am doing this as shown below:

g = nx.from_pandas_dataframe(df, 'node_from', 'node_to', edge_attr=['attribute1','attribute2','attribute3'])

df:

node_from:  node_to: ........ attribute1:   attribute2:   attribute3:
    jim      john    ........    tall          red             fat
    ...

All of the columns have words as their values, not digits.

I also have another dataframe which has each possible node and their attributes:

id:   attribute1:   attribute2:     attribute3:
jim      tall          red             fat
john     small         blue            fat
...

I essentially need to assign the above three attributes to their respective id, so every node has their 3 attributes attached.

Any help on how I could get node attributes working with my network is greatly appreciated.



from NetworkX - Setting node attributes from dataframe

No comments:

Post a Comment