简体   繁体   中英

How to create a knowledge graph from a pandas dataframe using RDFLIB library in Python

I have a df from which I need to create a Knowledge Graph using RDFlib library in Python. So that I can visualize the created knowledge graph in various visualization tools like Protege, Webowl etc. How can this be done? 在此处输入图像描述

Here is my approach based on this answer . See also this notebook for outputs.


import pandas as pd
from rdflib import Graph, URIRef, Namespace

d = {
    "source": pd.Series(["Edwin", "Reema", "Ron", "Tomorrow"]),
    "target": pd.Series(["football", "karate", "singer", "holiday"]),
    "edge": pd.Series(["plays", "plays", "is", "is"]),
}

df = pd.DataFrame(d)

g = Graph()
n = Namespace('http://example.org/foo/')

for inded, row in df.iterrows():
    # add triple to rdf-graph
    g.add((URIRef(n+row["source"]), URIRef(n+row["edge"]), URIRef(n+row["target"])))

print(g.serialize(format='turtle'))

With some additional visualization code (based on https://stackoverflow.com/a/61483971/333403 ) you arrive at: rdf图的可视化

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM