The pandas and networkx packages in Python can be used to transform the data from an XLS file into a directed graph. the subsequent actions:
Read the Excel file using pandas:
import pandas as pd
df = pd.read_excel('filename.xlsx')
Create a directed graph using networkx:
import networkx as nx
G = nx.DiGraph()
Iterate over the rows of the dataframe and add nodes and edges to the graph:
for index, row in df.iterrows():
source_node = row['Source']
target_node = row['Target']
weight = row['Weight']
G.add_edge(source_node, target_node, weight=weight)
The Excel file in this example is assumed to have three columns: Source, Target, and Weight. Weight denotes the weight/edge cost between two nodes with the labels Source and Target.
After that, you may use matplotlib or another library of your choosing to see the graph:
import matplotlib.pyplot as plt
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size=500)
nx.draw_networkx_edges(G, pos)
nx.draw_networkx_labels(G, pos)
plt.show()
This code will create a directed graph using the data in the Excel file and visualize it on the screen.