Question related to iteration over nodes and neighbours in networkx

Viewed 17

I am trying to model a networkx problem. Where I want to iterate over the all the nodes and their corresponding neighbours inside the node loop. g = my graph, u = certain node 20, df = dataset associated with the graph; where I extract the data corrosponding to 'u=20' in 'p'. details are shown below.

When I am trying to print this ; I am only getting the value of 'i' printed once ; while I expect it to be printed as many times as <'j'> there are rows in the column of my node (outer loop)

The results are also shown below.

u = 20
nb = nx.all_neighbors(g,u) # neighbours of node u
p = df.loc[df['userid'] == u] # extract entries of node u
r = len(p) # Av (Total number of tries u does to infect others)
count = 0 # counter for succesfull tries

if (r >0): # If the node has some enteries to infect others
  
    for column in p[['movieid']]: # extract the movie ID column for later comparison
      mid = p[column]
      
      for j in mid: # loop over all the movies of user u
        print ('\n',j )
        
        for i in nb: # loop over all the neibours of user u
            print (i)
            t = df.loc[df['userid'] == i]
            
            if( j in t['movieid'].unique()):# && ():
              count = count+1
              #break

An overview of all the neighbours of u = 20. enter image description here The p in the above is a data-frame I extracted corresponding to my 20th node 'u'. The p in the above is a data-frame I extracted corresponding to my 20th node 'u'.

Output:

 53497
1
2251
1024
1321
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
283

 62530

 61649
0 Answers
Related