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?

Math
  • 191
  • 2
  • 5
  • 19

1 Answers1

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
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Hi @Ynjxsjmh, I have got an error trying to run your code. I have opened a new question: https://stackoverflow.com/questions/67029198/typeerror-series-objects-are-mutable-thus-they-cannot-be-hashed Maybe you can help me (again). Thank you – Math Apr 09 '21 at 22:58