Search code examples
networkxnumpy-ndarray

Convert a numpy array of network values to a labelled node list with values


I have some graphs built with NetworkX with labelled nodes (names). I have computed trophic levels with the trophic tools script and obtained a numpy array of trophic values.

I want to create a node list of these values, with the according labels, similar for other topological indices (e.g. nx.degree_centrality is easily interpretable as every node names is followed by the relative value).

Can someone suggest how to merge or convert the numpy array to a labelled node list?

Thanks in advance!


Solution

  • I realised that the algorithm doesn't produce a real Laplacian and that every entry in the array was simply the node trophic value. I assumed the order of the array values as the same of the original node list, and the final data seems to concur with that (plant with lower values and predator with higher values).

    This is the code to join node names with their trophic values if someone need to compute a similar trophic index:

    trophic_levels = ta.trophic_levels(G)
    trophic_levels_list = list(trophic_levels)
    trophic_levels_series = pd.Series(trophic_levels_list)
    G_nodes = pd.DataFrame.from_dict(dict(G.nodes(data=True)), orient='index')
    G_nodes.reset_index(inplace=True)
    G_nodes['trophic_level'] = trophic_levels_series