Reputation: 704
I would need to create a network using the following data
String List1
string1 [string3, string2, string20, string4]
string2 [string100, string1, string4]
string3 [string1, string80, string2]
string4 [string13, string28, string12, string16]
string5 [string3, string8, string12, string6]
string6 []
with indirect links which shows links among the variables/nodes. So, for example, string1 is linked with string3, string2, string30, string4. String2 is linked with string100, string1 and string4, and so on.
I have tried to use Networkx
import itertools.combinations as comb
edges = set()
for col in df:
for _, data in df.groupby(col):
edges.update(comb(data.index, 2))
G = nx.Graph()
G.add_nodes_from(df.index)
G.add_edges_from(edges)
but it does not do what I would expect as output.
Upvotes: 0
Views: 93
Reputation: 9481
indeed you should use df.explode
. networkx
has a function that you can feed the exploded df into directly:
G = nx.from_pandas_edgelist(df.explode('List1'), source='String', target='List1')
That said, the way you construct the dataframe (i.e. lists as cell entries) causes a lot of headaches, so it is best avoided.
Upvotes: 2