0

I'd need to iterate a process across values in two different columns:

     A                      B                   Score      Value    
0   user1               test1                    6.6        A
1   user1               test2                    3.2        AA
2   user241             test1                    4.8        B
3   user12              test4                    3.1        C
4   user1               test1a                   2.9        A

Specifically, I'd need to link

- user1 with test1, test2 and test1a
- user241 with test1
- user 12 with test4
...

in order to create a network. I tried as follows

from pymnet import *
import matplotlib.pyplot as plt

mnet = MultilayerNetwork(aspects=1)
for i in df['A']:
    for j in df['B']:
        mnet[i, j,'friendship','friendship'] = 1

fig=draw(mnet, show=True, figsize=(25,30))

But it seems to not link A and B as expected. The problem is in the for condition.

Can you help me to figure out how to run the for loop correctly?

2
  • the variable df is undefined Commented Apr 4, 2021 at 2:59
  • I cannot understand, sorry, df is my pandas dataframe Commented Apr 4, 2021 at 3:00

1 Answer 1

3

With that double loop, you are creating connection among every A and every B.

enter image description here

You can do as following

for index in df.index:
    mnet[df.loc[index, 'A'], df.loc[index, 'B'],'friendship','friendship'] = 1

Or

for A, B in zip(df['A'], df['B']):
    mnet[A, B,'friendship','friendship'] = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Ynjxsjmh, I have got an error trying to run your code. I have opened a new question: stackoverflow.com/questions/67029198/… Maybe you can help me (again). Thank you

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.