I have a pandas dataframe of the the following form, df,
A B C D
A 0 0.5 0.5 0
B 1 0 0 0
C 0.8 0 0 0.2
D 0 0 1 0
I am trying to create a networkx graph from this. I have tried the following variations of code:
A)
G=networkx.from_pandas_adjacency(df)
G=networkx.DiGraph(G)
B)
G=networkx.from_pandas_adjacency(df, create_using=networkx.DiGraph())
However, what ends up happening is that the graph object either:
(For option A) basically just takes one of the values among the two parallel edges between any two given nodes, and deletes the other one.
(For option B) takes one of the values among the two parallel edges between any two given nodes, as the value for both edges.
For example,
print( list ( filter ( lambda x: x[0]=='A' and x[1] == 'B', list(G.edges.data()) ) ) )
and
print( list ( filter ( lambda x: x[0]=='B' and x[1] == 'A', list(G.edges.data()) ) ) )
prints 1 and [] for option A. prints two 1s for option B.
How do i resolve this issue?
Try using numpy as a workaround.
In a more general case, to get
label_mapping
you can useThis seems to be a bug in
networkx 2.0
. They will fix it in 2.1. See this issue for more information.