! pip install sqlalchemy
from kgforge.core import KnowledgeGraphForge
forge = KnowledgeGraphForge("../../configurations/forge.yml")
from kgforge.core import Resource
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("sqlite:///:memory:")
jane = Resource(name="Jane Doe", email="jane.doe@epfl.ch")
john = Resource(name="John Smith", email="john.smith@epfl.ch")
other = Resource(name="John Doe", email="john.doe@unige.ch")
persons = [jane, john, other]
Converting Resources to DataFrame
df = forge.as_dataframe(persons)
Persisting Resources in the SQL Database.
NB: The more powerful way is to have a Store implementation for the SQL Database.
df.to_sql("Persons", engine, index=False)
Getting the table as a DataFrame
pd.read_sql("SELECT * FROM Persons", engine)
Filtering on the SQL Database to keep only persons with an EPFL email
df = pd.read_sql("SELECT * FROM Persons WHERE email LIKE '%@epfl.ch'", engine)
df
Converting DataFrame to Resources
persons = forge.from_dataframe(df)
print(*persons, sep="\n")