Search code examples
pythongraphnetworkxnetwork-analysis

Cannot create a directed graph using from_pandas_dataframe from networkx


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.


Solution

  • Add the optional keyword argument create_using=nx.DiGraph(),

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