nebula.graph.ttypes.ExecutionResponse转pd.dataframe

nebula-python版本:1.1.1

执行client.execute_query查询,查到的数据类型为nebula.graph.ttypes.ExecutionResponse,我通过以下代码转成pd.DataFrame,但是数据量大的时候两层for逻辑的速度会特别慢,有较快的方法推荐吗?

nebulaObj=gClient.execute_query("XXXXX")
if nebulaObj.column_names is not None:
    columnList = [colItem.decode("utf8") for colItem in nebulaObj.column_names]
else:
    return pd.DataFrame([])
dataList = []
if nebulaObj.rows is not None:
    for rowItem in nebulaObj.rows:
        rowList = []
        for colItem in rowItem.columns:
            if type(colItem.value) == bytes:
                rowList.append(colItem.value.decode("utf8"))
            else:
                rowList.append(colItem.value)
        dataList.append(rowList.copy())
else:
    return pd.DataFrame([])
return pd.DataFrame(dataList, columns=columnList).drop_duplicates()

这里的value是啥类型?

是具体的图数据库里面的数据了~
现在我这个代码是跑得通的,但是主要问题是:因为双循环(循环遍历行和列),所以数据量大的时候获取数据特别慢,是否有更有效的方法呢?

没有更好的方法, 因为返回的结果全部都是用户需要的, 所以遍历无法避免. 如果有不需要的数据在返回的结果中建议看看 query 哪里能优化. 另外客户端提供了一些数据结构的 wrapper 和方法用来更方便取数据, 可以参考 nebula-python/test_data_type.py at master · vesoft-inc/nebula-python · GitHub

1 个赞

此话题已在最后回复的 7 天后被自动关闭。不再允许新回复。