简体   繁体   English

Networkx TypeError:输入图不是 networkx 图类型

[英]Networkx TypeError: Input graph is not a networkx graph type

I am trying to create a directed graph using from_pandas_edgelist .我正在尝试使用from_pandas_edgelist创建有向图。 I'm getting an error while passing the parameter create_using=nx.DiGraph()传递参数create_using=nx.DiGraph()出现错误

1) I tried creating a directed graph object and then tried to create a graph using from_pandas_edgelist : but it creates all links as bi-directional 1)我尝试创建一个有向图对象,然后尝试使用from_pandas_edgelist创建一个图:但它将所有链接创建为双向

G=nx.DiGraph()    G=nx.from_pandas_edgelist(df_SearchRelations,source='ParentCompanyId',target='ChildCompanyId',edge_attr=True)

2) I tried creating a graph object and then converting using to_directed() , but no luck 2)我尝试创建一个图形对象,然后使用to_directed()进行转换,但没有运气

G=nx.from_pandas_edgelist(df_SearchRelations,source='From',target='To',edge_attr=True)
G=G.to_directed()

Expected result: overcome the error of TypeError: Input graph is not a networkx graph type预期结果:克服TypeError: Input graph is not a networkx graph type

PS: the same code used to work, but is suddenly failing. PS:同样的代码曾经可以工作,但突然失败了。 I am unsure if it is a version compatibility issue or an issue with the dataframe.我不确定这是版本兼容性问题还是数据框问题。

Try using create_using parameter:尝试使用create_using参数:

G = nx.from_pandas_edgelist(df_SearchRelations,
                            source='ParentCompanyId',
                            target='ChildCompanyId',
                            edge_attr=True, 
                            create_using=nx.DiGraph())

I found out that the Directed graph generator will through an error when every nodes are not connected in a graph.我发现当图中的每个节点都没有连接时,有向图生成器会出错。 so when we pass the edge_list as pandas dataframe, we should check whether there are any disconnected subgraphs/nodes.所以当我们将 edge_list 作为 pandas 数据帧传递时,我们应该检查是否有任何断开的子图/节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM