def to_pandas_edges(G, x_kw, y_kw, **kwargs):  # noqa: N803
    """
    Convert Graph edges to pandas DataFrame that's readable to Altair.
    """
    # Get all attributes in nodes
    attributes = ["source", "target", "x", "y", "edge", "pair"]
    for e in G.edges():
        attributes += list(G.edges[e].keys())
    attributes = list(set(attributes))

    # Build a dataframe for all edges and their attributes
    df = pd.DataFrame(index=range(G.size() * 2), columns=attributes)

    # Add node data to dataframe.
    for i, (n1, n2, d) in enumerate(G.edges(data=True)):
        idx = i * 2
        x = G.node[n1][x_kw]
        y = G.node[n1][y_kw]
        data1 = dict(
            edge=i, source=n1, target=n2, pair=(n1, n2), x=x, y=y, **d
        )

        data2 = dict(
            edge=i, source=n1, target=n2, pair=(n1, n2), x=x, y=y, **d
        )

        df.loc[idx] = data1
        df.loc[idx + 1] = data2

    return df
def to_pandas_edges ( G , x_kw , y_kw , ** kwargs ) : attributes = [ "source" , "target" , "x" , "y" , "edge" , "pair" ] for e in G . edges ( ) : attributes += list ( G . edges [ e ] . keys ( ) ) attributes = list ( set ( attributes ) ) df = pd . DataFrame ( index = range ( G . size ( ) * 2 ) , columns = attributes ) for i , ( n1 , n2 , d ) in enumerate ( G . edges ( data = True ) ) : idx = i * 2 x = G . node [ n1 ] [ x_kw ] y = G . node [ n1 ] [ y_kw ] data1 = dict ( edge = i , source = n1 , target = n2 , pair = ( n1 , n2 ) , x = x , y = y , ** d ) data2 = dict ( edge = i , source = n1 , target = n2 , pair = ( n1 , n2 ) , x = x , y = y , ** d ) df . loc [ idx ] = data1 df . loc [ idx + 1 ] = data2 return df
Convert Graph edges to pandas DataFrame that's readable to Altair.