如何为节点创建边缘? [英] how to create edges for nodes?

查看:102
本文介绍了如何为节点创建边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在输入文件中找到每种蛋白质的程度,如下所示

In need a find the degree of every protein in the input file which is as shown below

A   B
a   b
c   d
a   c
c   b

我已经使用networkx来获取节点.如何在创建的节点上使用输入文件创建边缘?

I have used networkx to get the nodes. How do I create the edges using my input file on the created nodes?

代码:

import pandas as pd
df = pd.read_csv('protein.txt',sep='\t', index_col =0)
df = df.reset_index()
df.columns = ['a', 'b']

distinct = pd.concat([df['a'], df['b']]).unique()

import networkx as nx
G=nx.Graph()

nodes= []
for i in distinct:
    node=G.add_node(1)
    nodes.append(node)

推荐答案

首先,错误地使用了函数read_csv来读取输入文件.列之间用空格而不是制表符隔开,因此sep应该是'\s+'而不是'\t'.另外,输入文件中没有索引列,因此参数index_col不应设置为0.

At first, the function read_csv was used incorrectly to read the input file. The columns are separated by spaces, not tab, thus sep should be '\s+' instead of '\t'. Also, there is no index column in the input file, thus the parameter index_col should not be set to 0.

在将输入文件正确读取为DataFrame之后,我们可以使用

After having correctly read the input file into a DataFrame, we can convert it to a networkx graph using the function from_pandas_edgelist.

import networkx as nx
import pandas as pd

df = pd.read_csv('protein.txt', sep='\s+')
g = nx.from_pandas_edgelist(df, 'A', 'B')

这篇关于如何为节点创建边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆