converting a pandas dataframe to a networkx graph

  • Last Update :
  • Techknowledgy :
>>>
import pandas as pd
   >>>
   import numpy as np >>>
   r = np.random.RandomState(seed = 5) >>>
   ints = r.random_integers(1, 10, size = (3, 2)) >>>
   a = ['A', 'B', 'C'] >>>
   b = ['D', 'A', 'E'] >>>
   df = pd.DataFrame(ints, columns = ['weight', 'cost']) >>>
   df[0] = a >>>
   df['b'] = b >>>
   df
weight cost 0 b
0 4 7 A D
1 7 1 B A
2 10 9 C E
   >>>
   G = nx.from_pandas_dataframe(df, 0, 'b', ['weight', 'cost']) >>>
   G['E']['C']['weight']
10
   >>>
   G['E']['C']['cost']
9

Suggestion : 2

This post aims to describe how to draw a basic network chart using the networkx library of python. An example of a network chart with 5 nodes is plotted.,This example is probably the most basic network chart you can draw.,A network chart is constituted by nodes. These nodes are interconnected by edges. So a basic format is a data frame where each line describes a connection.,Here we construct a data frame with 4 lines, describing the 4 connections of this plot! So if you have a csv file with your connections, load it and you are ready to visualise it!

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

# Build a dataframe with 4 connections
df = pd.DataFrame({
   'from': ['A', 'B', 'C', 'A'],
   'to': ['D', 'A', 'E', 'C']
})

# Build your graph
G = nx.from_pandas_edgelist(df, 'from', 'to')

# Plot it
nx.draw(G, with_labels = True)
plt.show()

Suggestion : 3

You can define your edges by defining all pairs of nodes that have to same value for 'X' and the same value for 'Y' using itertools.combinations.,How can I get branch of a networkx graph from pandas dataframe in Python in the form of a new pandas dataframe?,Converting a pandas dataframe to a networkx graph,How can I get branch of a networkx graph as a list from pandas dataframe in Python?

You can define your edges by defining all pairs of nodes that have to same value for 'X' and the same value for 'Y' using itertools.combinations.

import itertools.combinations as comb

edges = set()
for col in df:
   for _, data in df.groupby(col):
   edges.update(comb(data.index, 2))

G = nx.Graph()
G.add_nodes_from(df.index)
G.add_edges_from(edges)

Your indexes are the labels for your nodes. So, we need to to reshape dataframe a bit to create an edge list dataframe:

d1 = df.reset_index().set_index(['X', df.groupby('X').cumcount()]).unstack()['index']
d2 = df.reset_index().set_index(['Y', df.groupby('Y').cumcount()]).unstack()['index']
d3 = pd.concat([d1, d2]).set_axis(['source', 'target'], inplace = False, axis = 1).dropna().astype(int)

G = nx.from_pandas_edgelist(d3, source = 'source', target = 'target')
nx.draw_networkx(G)

Suggestion : 4

May 23, 2022May 23, 2022,May 5, 2022May 5, 2022,March 28, 2022March 28, 2022

# Edges
df_edges = pd.DataFrame({
   'source': ['John', 'John', 'Jane', 'Alice', 'Tom', 'Helen', 'William'],
   'target': ['Jane', 'Helen', 'Tom', 'Tom', 'Helen', 'William', 'Alice'],
   'years': [2, 3, 5, 1, 2, 8, 3]
})
# Nodes
df_nodes = pd.DataFrame({
   'Name': ['John', 'Jane', 'Alice', 'Tom', 'Helen', 'William'],
   'Gender': ['Male', 'Female', 'Female', 'Male', 'Female', 'Male']
})
node_colors = {
   'Male': 'blue',
   'Female': 'red'
}
df_nodes['node_color'] = df_nodes['Gender'].map(node_colors)
G = nx.from_pandas_edgelist(df_edges, source = 'source', target = 'target', edge_attr = 'years')
nodes_attr = df_nodes.set_index('Name').to_dict(orient = 'index')
nx.set_node_attributes(G, nodes_attr)
plt.figure(figsize = (6, 6))
nx.draw_networkx(G,
   pos = nx.kamada_kawai_layout(G, weight = 'years'),
   node_size = 200,
   node_color = [G.nodes[n]['node_color']
      for n in G.nodes
   ],
   width = [G.edges[e]['years']
      for e in G.edges
   ],
   with_labels = True)
plt.plot()