如何在Graphistry中处理节点在多个列中出现时的合并。

huangapple go评论93阅读模式
英文:

How to merge nodes when they appear in multiple columns in Graphistry

问题

我正在尝试通过Graphistry从这个数据框构建一个超图:

data = [
    ["Jack", "Lauren", "Brian"],
    ["Lauren", "Brian", "Jaden"],
    ["Brian", "Jaden", "Tessa"],
]

names_df = pd.DataFrame(data, columns=["Previous", "Current", "Next"])

hg1 = graphistry.hypergraph(names_df, entity_types=["Previous", "Current", "Next"])

hg1_g = hg1["graph"]
hg1_g.plot()

问题是,节点在不同列中的每次出现时都被视为不同的节点。

我想要得到5个不同的节点和3条边,每个边对应数据框中的一行。

英文:

I am trying to construct a hypergraph via Graphistry from this dataframe:

data = [
    ["Jack", "Lauren", "Brian"],
    ["Lauren", "Brian", "Jaden"],
    ["Brian", "Jaden", "Tessa"],
]

names_df = pd.DataFrame(data, columns=["Previous", "Current", "Next"])

hg1 = graphistry.hypergraph(names_df, entity_types=["Previous", "Current", "Next"])

hg1_g = hg1["graph"]
hg1_g.plot()

The problem is that the nodes are treated as different each time they appear in the various columns.

I would like to get 5 different nodes and 3 edges, one for each row in the dataframe.

答案1

得分: 2

如果您想合并出现在不同列中的节点,可以使用opts关键字参数指定类别。此外,您可以通过指定direct=True在节点之间直接绘制边缘:

data = [
    ["Jack", "Lauren", "Brian"],
    ["Lauren", "Brian", "Jaden"],
    ["Brian", "Jaden", "Tessa"],
]

names_df = pd.DataFrame(data, columns=["Previous", "Current", "Next"])

hg1 = graphistry.hypergraph(names_df,
                            entity_types=["Previous", "Current", "Next"],
                            direct=True,
                            opts={
                                'CATEGORIES': {
                                    'person': ["Previous", "Current", "Next"]
                                },
                                'EDGES': {
                                    "Previous": ["Current"],
                                    "Current": ["Next"]
                                }
                            }
)

hg1_g = hg1["graph"]
hg1_g.plot()

编辑:您还可以在opts内部使用'EDGES'指定您的边缘。请参阅hypergraphdocstring

请注意,在这种情况下,您无需指定实体类型,因为您正在使用所有列。

英文:

If you want to merge node that appear in different columns, you can specify categories with the opts kwarg. Moreover, you can draw your edges directly between nodes by specifying direct=True:

data = [
    ["Jack", "Lauren", "Brian"],
    ["Lauren", "Brian", "Jaden"],
    ["Brian", "Jaden", "Tessa"],
]

names_df = pd.DataFrame(data, columns=["Previous", "Current", "Next"])

hg1 = graphistry.hypergraph(names_df,
                            entity_types=["Previous", "Current", "Next"],
                            direct=True,
                            opts={
                                'CATEGORIES': {
                                    'person': ["Previous", "Current", "Next"]
                                    },
                                'EDGES': {
                                    "Previous": ["Current"],
                                    "Current": ["Next"]
                                }
                                }
                            )

hg1_g = hg1["graph"]
hg1_g.plot()

Edit: you can also specify your edges with 'EDGES' inside opts. See hypergraph docstring

Note that you don't need to specify entity type in this case since you're using all columns.

huangapple
  • 本文由 发表于 2023年2月18日 01:04:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定