My goal is to create a knowledge graph using a csv file which includes, source, edge and target. What I have tried so far:
- it is not visible in the image, but I have two edges: 1) used for 2) related to.
- I have target tuples with 20 words.
first image is what I would like to see as a format. second image is the head of my csv data file, the third image shows the failed graph visualization as a result of this code.
# create a directed-graph from a dataframe
import networkx as nx
G=nx.from_pandas_edgelist(tuple_predictions_IB_for_graph, "source", "target",
edge_attr=True, create_using=nx.MultiDiGraph())
import matplotlib.pyplot as plt
plt.figure(figsize=(12,12))
pos = nx.spring_layout(G)
nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
plt.show()
You should use the
explode
method of your dataframe to make an entry for each target in your rows so that each target aligns with its appropriate source, then you'll get the nodes as desired.