英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论