Networkx KeyError: 'source' with from_pandas_edgelist for undirected edgelist


I have an edgelist in a pandas dataframe that looks like this:

    topic   neighbor
0   K       Kl
1   K       Pr
2   Kl      TS
3   Pr      Kl
4   Pr      Pr

When I turn this into a Graph (using networkx as nx) with G = nx.from_pandas_edgelist(df) it gives me KeyError: ‘source’.

It works when I specify a source and target G = nx.from_pandas_edgelist(df, "topic", "neighbor") but this is an undirected Graph so I do not really want a source and target.

Is this the way it has to be done? Will specifying a source and target have implications for later calculations of degree_centrality?

Asked By: noahjb



Yes, creating an undirected network from a dataframe requires specifying source and target.

It’s not necessary, but to be sure that the graph is undirected, one can specify create_using kwarg:

from networkx import Graph, from_pandas_edgelist

df = ...

# note that Graph is the default setting, so specifying
# create_using=Graph is optional
G = from_pandas_edgelist(df, "topic", "neighbor", create_using=Graph)

# False
Answered By: SultanOrazbayev

Try this:

import pandas as pd
import networkx as nx

df = pd.read_clipboard()


  topic neighbor
0     K       Kl
1     K       Pr
2    Kl       TS
3    Pr       Kl
4    Pr       Pr

Use source and target parameters:

G = nx.from_pandas_edgelist(df, source='topic', target='neighbor')


enter image description here

Answered By: Scott Boston
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.