For循环(嵌套):如何遍历两列?
For loop (nested): how to iterate through two 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
具体来说,我需要 link
- user1 with test1, test2 and test1a
- user241 with test1
- user 12 with test4
...
为了创建网络。
我尝试如下
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))
但似乎link A 和 B 没有预期的那样。
问题出在 for
条件下。
你能帮我弄清楚如何正确 运行 for
循环吗?
通过那个双循环,您将在每个 A 和每个 B 之间创建连接。
您可以按照以下方式进行
for index in df.index:
mnet[df.loc[index, 'A'], df.loc[index, 'B'],'friendship','friendship'] = 1
或者
for A, B in zip(df['A'], df['B']):
mnet[A, B,'friendship','friendship'] = 1
我需要在两个不同列中的值之间迭代一个过程:
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
具体来说,我需要 link
- user1 with test1, test2 and test1a
- user241 with test1
- user 12 with test4
...
为了创建网络。 我尝试如下
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))
但似乎link A 和 B 没有预期的那样。
问题出在 for
条件下。
你能帮我弄清楚如何正确 运行 for
循环吗?
通过那个双循环,您将在每个 A 和每个 B 之间创建连接。
您可以按照以下方式进行
for index in df.index:
mnet[df.loc[index, 'A'], df.loc[index, 'B'],'friendship','friendship'] = 1
或者
for A, B in zip(df['A'], df['B']):
mnet[A, B,'friendship','friendship'] = 1