更新 GraphFrame 中的顶点值

问题描述

我想知道在用 GraphFrame 构建图形后有什么方法可以更新顶点(或边)值?我有一个图,它的顶点有这些 ['id','name','age'] 列。我写了一个代码来创建新时代的顶点,它工作得非常好。但是,当我想将这些新顶点分配给旧图的顶点时,我收到了 can't set attribute 错误

from graphframes import GraphFrame
import pyspark.sql.functions as F

# Vertice DataFrame
v = spark.createDataFrame([
  ("a","Alice",34),("b","Bob",36),("c","Charlie",30),],["id","name","age"])

# Edge DataFrame
e = spark.createDataFrame([
  ("a","b","friend"),"c","follow"),["src","dst","relationship"])

# Create a GraphFrame
g = GraphFrame(v,e)

# Update Vertices
updated_vertices = (
    g.vertices
    .withColumn('new_age',F.lit(10))
    .select(
        'id',F.col('new_age').alias('age')
    )
)

# Set new vertices
g.vertices = updated_vertices

属性错误:无法设置属性

我应该重建一个新的图形对象吗?或者有没有更好的方法来做到这一点?

谢谢。

解决方法

您必须创建一个新的图形对象才能更新。但是,由于图形框对象只有两个数据框,您可以像

一样更新
g = GraphFrame(updated_vertices,e)

所以保持相同的名字