21

I'm learning networkx library and use twitter retweet directed graph data. I first read the datasets into pandas df (columns are 'from','to','weight') and wanted to put a first 300 rows(retweet) into a graph using below code:

tw_small = nx.from_pandas_dataframe(edges_df[:300],source='from',
                                   target='to',edge_attr=True)

I thought that it correctly created a graph but when I run tw_small.is_directed(), it says False(undirected graph) and I drew a graph using nx.draw() but it doesn't show the direction either.

Could someone help me find a correct way to make a directed graph?

Thanks.

4

2 回答 2

36

添加可选的关键字参数 create_using=nx.DiGraph(),

tw_small = nx.from_pandas_dataframe(edges_df[:300],source='from',
                                   target='to',edge_attr=True,
                                   create_using=nx.DiGraph())
于 2017-06-18T17:06:23.713 回答
9

您可以编写 edgelist 而不是数据框,它对我有用,当我使用from_pandas_dataframe : "AttributeError: module 'networkx' has no attribute 'from_pandas_dataframe

解决方案 :

Graph = nx.from_pandas_edgelist(df,source='source',target='destination', edge_attr=None, create_using=nx.DiGraph())

您可以测试您的图表是否是定向的:nx.is_directed(Graph). 你会得到真实的。

于 2019-04-30T18:54:53.780 回答