7

I am contructing a networkx graph in python 3. I am using a pandas dataframe to supply the edges and nodes to the graph. Here is what I have done :

test = pd.read_csv("/home/Desktop/test_call1", delimiter = ';')

g_test = nx.from_pandas_edgelist(test, 'number', 'contactNumber', edge_attr='callDuration')

What I want is that the "callDuration" column of the pandas dataframe act as the weight of the edges for the networkx graph and the thickness of the edges also change accordingly.

I also want to get the 'n' maximum weighted edges.

Anand Nautiyal
  • 245
  • 2
  • 5
  • 11

2 Answers2

8

Let's try:

import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

df = pd.DataFrame({'number':['123','234','345'],'contactnumber':['234','345','123'],'callduration':[1,2,4]})

df

G = nx.from_pandas_edgelist(df,'number','contactnumber', edge_attr='callduration')
durations = [i['callduration'] for i in dict(G.edges).values()]
labels = [i for i in dict(G.nodes).keys()]
labels = {i:i for i in dict(G.nodes).keys()}

fig, ax = plt.subplots(figsize=(12,5))
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, ax = ax, labels=True)
nx.draw_networkx_edges(G, pos, width=durations, ax=ax)
_ = nx.draw_networkx_labels(G, pos, labels, ax=ax)

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks Scott. I have multiple rows where "number" and "contactnumber" columns have the same values and "callDuration column varies. For instance, the values are like {'number':['123','123','345'],'contactnumber':['234','234','567'],'callduration':[1,2,4]}). So, I want to add up the callDuration for the first two rows so that they can be represented as a single edge in the graph. Please tell me how can I achieve this. – Anand Nautiyal Sep 21 '18 at 04:33
  • Hi Scott. Well, I have carried out the adding of rows based on similarity of some attributes in pandas. It is completed. However, I am still finding it difficult to find out the edges with maximum weights. Is there a way to get 'n' maximum weighted edges?? – Anand Nautiyal Sep 21 '18 at 05:41
  • Well, @AnandNautiyal you could simply query your input data frame for max callduration. `df[df.callduration.max() == df.callduration]` – Scott Boston Sep 21 '18 at 18:04
  • 1
    Yes Scott, that can be done. But I wanted to use the edge properties for finding out the maximum as I will be using the graph object for finding trends later on. Thanks. – Anand Nautiyal Sep 24 '18 at 09:48
  • @AnandNautiyal Ok, I think you got your answer in https://stackoverflow.com/a/52445069/6361531. – Scott Boston Sep 24 '18 at 13:29
3

Do not agree with what has been said. In the calcul of different metrics that takes into account the weight of each edges like the pagerank or the betweeness centrality your weight would not be taking into account if is store as an edge attributes. Use graph.

Add_edges(source, target, weight, *attrs)

Venkatachalam
  • 16,288
  • 9
  • 49
  • 77