Search code examples
pythonpandasgraphnetworkxgraph-theory

How to create a edgelist from a pandas datrafame?


I have a pandas dataframe (df) with 1: connected vertices and 0: unconnected vertices

X B C D
F 1 1 0
G 0 0 1
H 0 1 0

I need to convert this to a edgelist like:

Source Target
F B
F C
G D
H C

What is the best way to do this?


Solution

  • Here is a way using stack() and constructing a new df.

    df2 = (pd.DataFrame(
        df.set_index('X').where(lambda x: x.ne(0)).stack().index.tolist(),
        columns = ['X','Target']))
    

    or

    df.set_index('X').mul(df.columns[1:]).where(lambda x: x.ne('')).stack().droplevel(1).reset_index().set_axis(['X','Target'],axis=1)
    

    or

    df.set_index('X').dot(df.columns[1:] + ',').str.strip(',').str.split(',').explode().reset_index().set_axis(['X','Target'],axis=1)
    

    Output:

       X Target
    0  F      B
    1  F      C
    2  G      D
    3  H      C