'Why does my for loop preemptively break when attempting to display a Pandas dataframe data in a NetworkX graph?

I'm attempting to connect information gathered about hashes to their various C2 servers, and am reading it from a CSV file into a dataframe, and then adding the nodes which are the headers (the hashes) and then in their individual columns are several IPs/URLs (not a set amount). When outputting this to a graph, it only displays the first row of information though, which makes me think my for loop setting the nodes and edges up is broken somehow, but I can't figure out how, as the logic seems like it should be okay for different numbers of rows per column, but at the same time the issue may be dropping the NaN from the dataframe.

import networkx as nx
from networkx.drawing.nx_agraph import write_dot
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
from pyparsing import col, with_attribute
import pandas as pd

files = 'exampleCSV.csv'
df = pd.read_csv(files)
df = df._convert(numeric=True)
df = df.drop('sampleHash', 1)
df = df.dropna()

headerList = list(df.columns.values)
print(headerList)

g = nx.Graph()

for i in range(len(headerList)):
    g.add_node(headerList[i])
    valueList = df[headerList[i]].tolist()
    
    for x in range(len(valueList)):
        g.add_node(valueList[x])
        g.add_edge(headerList[i], valueList[x]) 

I've included photos of the outputted graph and the CSV file it is pulling information from. I also tried directly doing the dataframe to networkx conversion function, and it broke everything really badly, so individually adding and linking them in a loop is the best way I've found to do this so far.

Outputted Graph:

Outputted Graph

CSV File:

CSV File



Solution 1:[1]

Thanks to @Barmar for his tips, it was an issue with my logic, and dropping NaN caused some issues as well. Aside from some bad coding practices of mine, here is the initial fix that worked out:

files = 'exampleCSV.csv'
df = pd.read_csv(files)
df = df._convert(numeric=True)
df = df.drop('sampleHash', 1)

headerList = list(df.columns.values)

g = nx.Graph()

for i in range(len(headerList)):
    g.add_node(headerList[i])
    
    valueList = df[headerList[i]].tolist()
    
    for x in range(len(valueList)):
        try:
            result = math.isnan(valueList[x])
        except:
            result = False
        if result == False:
            g.add_node(valueList[x])
            print(valueList[x])
            g.add_edge(headerList[i], valueList[x]) 
        

NaN was keeping it iterating as the max row of some of the values was only one row, so the for loop was most likely unable to continue on. The NaNs from Pandas in the open spots in the lists made iteration easier, so adding them in and just filtering them out when creating the graph solved the issue.

I'm sure there is a cleaner way of doing this, but it does what it's supposed to now. Thanks for the help!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evan