[英]Failed to Convert Edge Data Network X
I have this small-gephiready.tsv file that structured like this:我有一个结构如下的 small-gephiready.tsv 文件:
Source Target Type
1 2 Undirected
1 3 Undirected
1 4 Undirected
1 5 Undirected
1 6 Undirected
I want to open it via NetworkX in python environment, with code:我想在python环境下通过NetworkX打开,代码:
G = nx.read_edgelist("small-gephiready.tsv", nodetype=int)
However, it returns error "Failed to convert edge data (['Type']) to dictionary".但是,它返回错误“无法将边数据 (['Type']) 转换为字典”。 I thought Type datas is string, so I added
data=('Type',str)
after nodetype=int, and it returns str has no attribute decode.我以为Type datas是string,所以我在nodetype=int后面加了
data=('Type',str)
,返回str has no attribute decode。
Am I write the read_edge function wrong or there is something I have to checked in file?我是写了 read_edge function 错误还是我必须检查文件? Thank you for your help.
感谢您的帮助。
This is my answer, that apparently if you give headers for a file, you need some parameters to read them.这是我的回答,显然如果你给一个文件的标题,你需要一些参数来读取它们。 However, I need pandas read_csv function and toss it to network_x with from_pandas_edgelist function.
但是,我需要 pandas read_csv function 并将其与 from_pandas_edgelist function 一起扔到 network_x。
f = pd.read_csv("small-gephiready.tsv", sep='\t')
G = nx.from_pandas_edgelist(f, source='FromNodeId', target='ToNodeId')
I am not really sure, how it worked, but the below code seemed to have provided me with expected results:我不太确定它是如何工作的,但下面的代码似乎为我提供了预期的结果:
Remove the Headers first, then use the below code先删除标题,然后使用以下代码
nx.read_edgelist("small-gephiready.tsv", nodetype=int, data=(("Type", str),))
It loaded the nodes and edges without any issue.它毫无问题地加载了节点和边缘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.