Answer a question

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'])

The network will be a directed network. The attributes shown in the below dataframe are the attributes for the 'node_from' nodes. The 'node_to' nodes sometimes appear as 'node_from' nodes. All the nodes that can possibly be shown in the network and their respective attributes are shown in the df_attributes_only table.

df_relationship:

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:

df_attributes_only:

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.

Answers

As of Networkx 2.0, you can input a dictionary of dictionaries into nx.set_node_attributes to set attributes for multiple nodes. This is a much more streamlined approach compared to iterating over each node manually. The outer dictionary keys represent each node, and the inner dictionaries keys correspond to the attributes you want to set for each node. Something like this:

attrs = {
    node0: {attr0: val00, attr1: val01},
    node1: {attr0: val10, attr1: val11},
    node2: {attr0: val20, attr1: val21},
}
nx.set_node_attributes(G, attrs)

You can find more detail in the documentation.


Using your example, assuming your index is id, you can convert your dataframe df_attributes_only of node attributes to this format and add to your graph:

df_attributes_only = pd.DataFrame(
    [['jim', 'tall', 'red', 'fat'], ['john', 'small', 'blue', 'fat']],
    columns=['id', 'attribute1', 'attribute2', 'attribute3']
)
node_attr = df_attributes_only.set_index('id').to_dict('index')
nx.set_node_attributes(g, node_attr)

g.nodes['jim']


>>> {'attribute1': 'tall', 'attribute2': 'red', 'attribute3': 'fat'}
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐