Issue
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?
Solution
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
Answered By - SultanOrazbayev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.