2

Starting from this simple dataframe:

  node   t1   t2
0    a  pos  neg
1    b  neg  neg
2    c  neg  neg
3    d  pos  neg
4    e  neg  pos
5    f  pos  neg
6    g  neg  pos

I would like to build an edgelist file to read it as a undirected network. The expected output is:

b c
a d
a f
d f
e g

So basically I'm linking two nodes if they have the same pair of values in the ['t1','t2'] columns. So far I tried first to group the values into one new column:

d['c'] = [tuple(i) for i in df[['t1','t2']].values]

But then I'm stucked in grouping the users as I wish.

EDIT: fix error in the creation of the new column.

1 Answer 1

3

Have a look at this:

df = pd.DataFrame({'node': ['a', 'b','c', 'd', 'e', 'f', 'g'],
               't1': ['pos', 'neg', 'neg', 'pos', 'neg', 'pos', 'neg'],
               't2': ['neg', 'neg', 'neg', 'neg', 'pos', 'neg', 'pos']})

K = nx.Graph()
K.add_nodes_from(df['node'].values)

# Create edges
for i, group in df.groupby(['t1', 't2'])['node']:
    # generate all combinations without replacement 
    # from the group of similar column pairs
    for u, v in itertools.combinations(group, 2):           
        K.add_edge(u, v)

print(K.edges())

Result: [('a', 'd'), ('a', 'f'), ('c', 'b'), ('e', 'g'), ('d', 'f')]

The trick here is to group by the 2 columns at the same time in pandas. Then, you can just create all combinations of edges to add in the graph.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.