Page MenuHomePhabricator

Add Python -> Pandas to Code in WDQS
Open, Needs TriagePublic

Description

See Jupyter example

As data scientist are a target group of using Wikidata and python combined with pandas is used a lot

I suggest that we add one more option to WDQS Code "Python -> Pandas"

image.png (2,774×1,350 px, 403 KB)

image.png (2,018×1,792 px, 367 KB)

My understanding is that we do the same as option Python but a new procedure is used get_sparql_dataframe see Jupyter example

def get_sparql_dataframe(endpoint_url, query):
    """
    Helper function to convert SPARQL results into a Pandas data frame.
    """
    user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
    # TODO adjust user agent; see https://w.wiki/CX6

    sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    result = sparql.query()

    processed_results = json.load(result.response)
    cols = processed_results['head']['vars']

    out = []
    for row in processed_results['results']['bindings']:
        item = []
        for c in cols:
            item.append(row.get(c, {}).get('value'))
        out.append(item)
    return pd.DataFrame(out, columns=cols)