Search code examples
pythonpandasnetworkxgraphviz

Social graph: Pandas dataframe to Networkx graph


I have a dataset that contains a user and each friend they have. The format is similar to the below where there is a user_id field and friend_id field containing the corresponding IDs of each friend.

user_id | friend_id
   A          B
   A          C
   B          A
   B          G

I'm aiming to get an undirected graph showing an edge between each user and every friend they have like below.

A - B - G
|
C

I'm having difficulty finding out how to link pandas to networkx or graphviz and other resources that expand on creating a social graph from tabular data.


Solution

  • As an example here is a way to show an undirected network graph using networkx, pandas, and matplotlib.

    Code:

    import matplotlib.pyplot as plt
    import networkx as nx
    
    # store pairs to a list of tuples
    tuples = [tuple(x) for x in df.values]
    
    # set up a graph and show it
    G = nx.DiGraph()
    G.add_edges_from(tuples)
    nx.draw_networkx(G)
    plt.xticks([], [])
    plt.yticks([], [])
    plt.show()
    

    Output:

    enter image description here