将Pandas数据框转换为包含ID和权重的元组列表 [英] Convert Pandas data frame to a list of tuples containing IDs and a weight
问题描述
我有一个数据帧(称为df),其当前格式如下:
I have a data frame (called df) which is currently formatted like so:
1 2 3
1 1 0.26 0.02
2 0.26 1 0.61
3 0.02 0.61 1
这些ID由一个值连接,我想以某种方式提取所有唯一的ID值,以便以更有效的方式将它们添加到networkx上的图形中.
The IDs are connected by a value and I would like to somehow extract all unique ID values in order to have a more efficient way to add them to my graph on networkx.
输出应如下所示:
ed_list = [(1,2,{'weight': 0.26}),(1,3,{'weight': 0.02}),(2,3,{'weight':0.61})]
目前,我使用以下方法:
At the moment I use the following method:
# Create matrix
new_ = df.values
A_d = np.matrix(new_)
G = nx.from_numpy_matrix(A_d)
我想知道从df创建元组列表是否可以更容易/更有效,我可以用它来连接节点,然后在其中添加如下边:
I'm wondering if it would be easier/more efficient to create a List of tuples from my df that I could use to connect my nodes, where I could then add edges like so:
G.add_edges_from(ed_list)
我在上一个版本的问题中犯了一个错误-列名和行名只是整数
I have made a mistake in the previous version of my question - the column and row names are just integers
推荐答案
可以尝试:
# this s is what you are looking for
s = df.where(df.index.values > df.columns.values[:,None]).stack().reset_index(name='weight')
# we can use dataframe directly
G = nx.from_pandas_edgelist(s,source='level_0',target='level_1', edge_attr='weight')
或更简单:
G = nx.from_pandas_adjacency(df)
这篇关于将Pandas数据框转换为包含ID和权重的元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!