更新GraphFrame中的顶点值

2024-06-02 07:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道在用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"),
  ("b", "c", "follow"),
  ("c", "b", "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',
        'name',
        F.col('new_age').alias('age')
    )
)

# Set new vertices
g.vertices = updated_vertices

AttributeError: can't set attribute

我应该重建一个新的图形对象吗?还是有更好的方法

多谢各位


Tags: nameimportid图形dataframenewageattribute
1条回答
网友
1楼 · 发布于 2024-06-02 07:14:39

您必须创建一个新的图形对象才能进行更新。但是,由于graphframe对象只有两个数据帧,因此可以像这样进行更新

g = GraphFrame(updated_vertices, e)

所以保持相同的名字

相关问题 更多 >