import networkx as nx
# Create the graph from the dataframe
g = nx.Graph()
g.add_edges_from(df[['data1','data2']].itertuples(index=False))
,谢谢@Dyz:我有一个如下所示的数据框
data1=[202,701,410,301,402,201,202,301,310,210,302,302,401,402,601,303]
data2=[210,303,500,302,403,202,203,303,400,300,303,310,402,410,203,801]
df = pd.DataFrame(data1,columns = ['data1'])
df['data2'] = data2
这为我带来了数据框。现在,我需要将具有父子关系的两列进行分组,如下所示。
[201, 202, 203, 300, 210, 601],
[801, 301, 302, 303, 400, 310, 701],
[401, 402, 403, 500, 410]
我不需要确切的父母或确切的孩子。我只想对谁都属于python 3中的同一类别进行分组。
提前感谢
我具有如下数据帧data1 = [202,701,410,301,402,201,202,301,310,210,302,302,401,402,601,303,303] 400 = 300,303,310,310,402,410,203,801,303,400,300,303,310,402,410,203,801,400,300,303,310,310,402,410,203,801,400,300,303,310,310,402,410,203,801,df = pd。
networkx
与connected_components
一起使用,最后将输出转换为列表列表:connected_components
import networkx as nx
# Create the graph from the dataframe
g = nx.Graph()
g.add_edges_from(df[['data1','data2']].itertuples(index=False))
,谢谢@Dyz:convert_matrix.from_pandas_edgelist
convert_matrix.from_pandas_edgelist
import networkx as nx
# Create the graph from the dataframe
g = nx.Graph()
g.add_edges_from(df[['data1','data2']].itertuples(index=False))
,谢谢@Dyz: