如何仅从 Edge DataFrame 制作 GraphFrame

How to make GraphFrame from Edge DataFrame only

来自 this、"A GraphFrame can also be constructed from a single DataFrame containing edge information. The vertices will be inferred from the sources and destinations of the edges."

然而,当我查看它的 API doc 时,似乎无法创建一个。

有人试过只使用边缘 DataFrame 创建 GraphFrame 吗?怎么样?

graphframes scala API 有一个名为 fromEdges 的函数,它从边数据帧生成一个 graphframe。据我所知,此功能在 pyspark 中不可用,但您可以执行以下操作:

##something

verticesDf = edgesDF.select('src').union(edgesDF.select('dst'))
verticesDf = verticesDf.withColumnRenamed('src', 'id')

##more something

实现相同。

为了避免在顶点列表中重复,我会添加一个不同的

verticesDf=edgesDf \
     .select("src") \ 
     .union(edgesDf.select("dst")) \
     .distinct() \
     .withColumnRenamed('src', 'id')

verticesDf.show()

graph=GraphFrame(verticesDf,edgesDf)

这是另一种不读取整个数据两次的替代方法:

nodes = (
    edges
    .withColumn("id", F.explode(F.array(F.col("src"), F.col("dst"))))
    .select("id")
    .distinct()
)