Home > Software engineering >  Networkx KeyError: 'source' with from_pandas_edgelist for undirected edgelist
Networkx KeyError: 'source' with from_pandas_edgelist for undirected edgelist

Time:11-20

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?

CodePudding user response:

Try this:

import pandas as pd
import networkx as nx

df = pd.read_clipboard()
print(df)

Output:

  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')
nx.draw_networkx(G)

Output:

enter image description here

CodePudding user response:

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)


print(G.is_directed())
# False
  • Related