在 pandas DF中对连接的图进行分组 [英] Group connected graphs in pandas DF
问题描述
我有一个熊猫DF,其中每一列代表一个节点,两列代表一条边缘,如下所示:
I have a pandas DF where each column represent a node and two columns an edge, as following:
import pandas as pd
df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
'node2': ['4', '13', '25', '38', '208', '300']})
所有节点都是无向的,也就是说,您可以从一个到另一个 undirected_graph
All Nodes are Undirected, i.e. you can get from one to the other undirected_graph
我想将它们分为所有关联的组(连通性),如下:
I would like to group them into all connected groupes (Connectivity), as following:
df = pd.DataFrame({'node1': ['2', '4','17', '17', '205', '208'],
'node2': ['4', '13', '25', '38', '208', '300']
,'desired_group': ['1', '1', '2', '2', '3', '3']})
例如,对前两行进行分组的原因是因为它可以从节点2到达节点13(通过4).
For example, the reason why the first two rows were grouped, is because its possible to get from node 2 to node 13 (through 4).
我设法找到的最接近的问题是这个: 熊猫-根据列值将数据框重塑为边缘列表但据我了解,这是一个不同的问题.
The closest question that i managed to find is this one: pandas - reshape dataframe to edge list according to column values but to my understanding, its a different question.
在此方面提供任何帮助都将非常有用,谢谢.
Any help on this would be great, thanks in advance.
推荐答案
使用networkx
connected_components
import networkx as nx
G=nx.from_pandas_edgelist(df, 'node1', 'node2')
l=list(nx.connected_components(G))
L=[dict.fromkeys(y,x) for x, y in enumerate(l)]
d={k: v for d in L for k, v in d.items()}
#df['New']=df.node1.map(d)
df.node1.map(d)
0 0
1 0
2 1
3 1
4 2
5 2
Name: node1, dtype: int64
这篇关于在 pandas DF中对连接的图进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!